我试图访问和更改从JSON API调用收到的数据。访问并更改2d Json数组,Javascript
呼叫的格式是这样的:
{
"count": 391,
"value": [
{
"id": "id1",
"name": "name1",
"url": "url1",
"project": {
"id": "projId",
"name": "BT-GIT",
"url": "otherurl",
"state": "wellFormed"
},
"defaultBranch": "master",
"remoteUrl": "remote"
},
{
"id": "id2",
"name": "name2",
"url": "url2",
"project": {
"id": "projId",
"name": "BT-GIT",
"url": "otherurl",
"state": "wellFormed"
},
"defaultBranch": "master",
"remoteUrl": "remote"
},...
,我想一个额外的条目添加到每个“值”,这样我有:
{
"id": "id1",
"name": "name1",
"url": "url1",
"project": {
"id": "projId",
"name": "BT-GIT",
"url": "otherurl",
"state": "wellFormed"
},
"defaultBranch": "master",
"remoteUrl": "remote"
"date": "date" <---------
}
我试着:
$.each(data, function (idx) {
data.value[idx].currentDate = new Date().toJSON().slice(0, 16);
})
和:
$.each(data, function (idx) {
data[1][idx].currentDate = new Date().toJSON().slice(0, 16);
})
任何关于如何解决此问题的想法?
非常感谢。
它看起来像你迭代上'data',当它听起来像你想迭代'data.value'。 '$ .each(data.value,function ...'可能会诀窍 – neilsimp1
该文档解释了如何使用'$ .each'很清楚地迭代overan对象:http://api.jquery.com/jquery.each /#jQuery-each-object-callback – user3297291