![](/img/trans.png)
[英]How to get all child and sub child data into main parent data as a childData on loop in an array javascript
[英]How to get childData inside a key
我創建了一個腳本,在其中我根據其moduleId將數據分組。
這是示例json
{
"1": [{
"id": 1,
"moduleId": "1",
"dropdownModuleName": "NL",
"isDeleted": null,
"createdBy": "1",
"updatedBy": null,
"dateCreated": "2018-09-19 17:37:21",
"dateUpdated": null
}, {
"id": 2,
"moduleId": "1",
"dropdownModuleName": "EE",
"isDeleted": null,
"createdBy": null,
"updatedBy": null,
"dateCreated": "2018-09-19 18:01:39",
"dateUpdated": null
}, {
"id": 3,
"moduleId": "1",
"dropdownModuleName": "SA",
"isDeleted": null,
"createdBy": null,
"updatedBy": "1",
"dateCreated": "2018-09-19 18:01:46",
"dateUpdated": "2018-09-19 18:10:02"
}],
"4": [{
"id": 4,
"moduleId": "4",
"dropdownModuleName": "CON",
"isDeleted": null,
"createdBy": null,
"updatedBy": "1",
"dateCreated": "2018-09-19 18:01:56",
"dateUpdated": "2018-09-19 18:13:33"
}, {
"id": 5,
"moduleId": "4",
"dropdownModuleName": "XPO",
"isDeleted": null,
"createdBy": "1",
"updatedBy": null,
"dateCreated": "2018-09-19 18:38:41",
"dateUpdated": null
}, {
"id": 6,
"moduleId": "4",
"dropdownModuleName": "RG",
"isDeleted": null,
"createdBy": "1",
"updatedBy": null,
"dateCreated": "2018-09-19 19:13:00",
"dateUpdated": null
}],
"5": [{
"id": 7,
"moduleId": "5",
"dropdownModuleName": "REG",
"isDeleted": null,
"createdBy": "1",
"updatedBy": null,
"dateCreated": "2018-09-19 19:13:18",
"dateUpdated": null
}, {
"id": 8,
"moduleId": "5",
"dropdownModuleName": "COM",
"isDeleted": null,
"createdBy": "1",
"updatedBy": null,
"dateCreated": "2018-09-19 19:13:28",
"dateUpdated": null
}, {
"id": 9,
"moduleId": "5",
"dropdownModuleName": "BEE",
"isDeleted": null,
"createdBy": "1",
"updatedBy": null,
"dateCreated": "2018-09-19 19:13:35",
"dateUpdated": null
}, {
"id": 10,
"moduleId": "5",
"dropdownModuleName": "CA,
"isDeleted": null,
"createdBy": "1",
"updatedBy": null,
"dateCreated": "2018-09-19 19:13:44",
"dateUpdated": null
}, {
"id": 11,
"moduleId": "5",
"dropdownModuleName": "PPC",
"isDeleted": null,
"createdBy": "1",
"updatedBy": null,
"dateCreated": "2018-09-19 19:13:51",
"dateUpdated": null
}, {
"id": 12,
"moduleId": "5",
"dropdownModuleName": "TIC",
"isDeleted": null,
"createdBy": "1",
"updatedBy": null,
"dateCreated": "2018-09-19 19:13:58",
"dateUpdated": null
}]
}
我這樣做的方法是使用此腳本。
newBody = JSON.parse(body);
newJson = _.groupBy(newBody,function (o) {
return o.moduleId
})
nBody = JSON.stringify(newJson)
我想做的是在ajax成功請求后在json中獲取dropdownModuleName
, moduleId
等。所以我要做的就是循環結果。 發生了什么事,但是我的控制台上沒有顯示任何結果...
這是我的ajax請求。
$.ajax({
url:'/api/navbarToggleDropdownMenu/getall',
type:'GET',
dataType:'JSON',
success:function (res) {
console.log(res);
for (var i = 0; i < res.length; i++) {
console.log(res[i])
}
}
})
第一個控制台在我上面的帖子中返回json ..但是for循環內的控制台未返回結果
如果您得到上面的響應,則可能是問題所在
res[i]
res是基於花括號的對象。 id成為res的屬性,因此對其進行訪問將是res.1,但數字不符合javascript標識符/變量名。
嘗試為響應添加另一個級別,但使用其他密鑰?
編輯:或另一種方法,如循環屬性,例如
for (var property1 in res) {
res[property1]; } // here is the value
}
用這樣的東西替換你的循環
for (var i in res) {
console.log(res[i]);
}
var res = { "1": [{ "id": 1, "moduleId": "1", "dropdownModuleName": "NL", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 17:37:21", "dateUpdated": null }, { "id": 2, "moduleId": "1", "dropdownModuleName": "EE", "isDeleted": null, "createdBy": null, "updatedBy": null, "dateCreated": "2018-09-19 18:01:39", "dateUpdated": null }, { "id": 3, "moduleId": "1", "dropdownModuleName": "SA", "isDeleted": null, "createdBy": null, "updatedBy": "1", "dateCreated": "2018-09-19 18:01:46", "dateUpdated": "2018-09-19 18:10:02" }], "4": [{ "id": 4, "moduleId": "4", "dropdownModuleName": "CON", "isDeleted": null, "createdBy": null, "updatedBy": "1", "dateCreated": "2018-09-19 18:01:56", "dateUpdated": "2018-09-19 18:13:33" }, { "id": 5, "moduleId": "4", "dropdownModuleName": "XPO", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 18:38:41", "dateUpdated": null }, { "id": 6, "moduleId": "4", "dropdownModuleName": "RG", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:00", "dateUpdated": null }], "5": [{ "id": 7, "moduleId": "5", "dropdownModuleName": "REG", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:18", "dateUpdated": null }, { "id": 8, "moduleId": "5", "dropdownModuleName": "COM", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:28", "dateUpdated": null }, { "id": 9, "moduleId": "5", "dropdownModuleName": "BEE", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:35", "dateUpdated": null }, { "id": 10, "moduleId": "5", "dropdownModuleName": "CA", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:44", "dateUpdated": null }, { "id": 11, "moduleId": "5", "dropdownModuleName": "PPC", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:51", "dateUpdated": null }, { "id": 12, "moduleId": "5", "dropdownModuleName": "TIC", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:58", "dateUpdated": null }] } for (var i in res) { console.log(res[i]) }
您需要使用嵌套循環來找到子值...在下面的示例中,我為您$.each nested function
並打印console.log('moduleId-' + v.moduleId +' '+'dropdownModuleName-'+ v.dropdownModuleName)
var data = { "1": [{ "id": 1, "moduleId": "1", "dropdownModuleName": "NL", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 17:37:21", "dateUpdated": null }, { "id": 2, "moduleId": "1", "dropdownModuleName": "EE", "isDeleted": null, "createdBy": null, "updatedBy": null, "dateCreated": "2018-09-19 18:01:39", "dateUpdated": null }, { "id": 3, "moduleId": "1", "dropdownModuleName": "SA", "isDeleted": null, "createdBy": null, "updatedBy": "1", "dateCreated": "2018-09-19 18:01:46", "dateUpdated": "2018-09-19 18:10:02" }], "4": [{ "id": 4, "moduleId": "4", "dropdownModuleName": "CON", "isDeleted": null, "createdBy": null, "updatedBy": "1", "dateCreated": "2018-09-19 18:01:56", "dateUpdated": "2018-09-19 18:13:33" }, { "id": 5, "moduleId": "4", "dropdownModuleName": "XPO", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 18:38:41", "dateUpdated": null }, { "id": 6, "moduleId": "4", "dropdownModuleName": "RG", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:00", "dateUpdated": null }], "5": [{ "id": 7, "moduleId": "5", "dropdownModuleName": "REG", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:18", "dateUpdated": null }, { "id": 8, "moduleId": "5", "dropdownModuleName": "COM", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:28", "dateUpdated": null }, { "id": 9, "moduleId": "5", "dropdownModuleName": "BEE", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:35", "dateUpdated": null }, { "id": 10, "moduleId": "5", "dropdownModuleName": "CA", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:44", "dateUpdated": null }, { "id": 11, "moduleId": "5", "dropdownModuleName": "PPC", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:51", "dateUpdated": null }, { "id": 12, "moduleId": "5", "dropdownModuleName": "TIC", "isDeleted": null, "createdBy": "1", "updatedBy": null, "dateCreated": "2018-09-19 19:13:58", "dateUpdated": null }] } $.each(data, function(key, value){ $.each(value, function(k,v){ console.log('moduleId-' + v.moduleId +' '+'dropdownModuleName-'+ v.dropdownModuleName) }) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
嘗試以下代碼可能會在您的控制台中獲得結果。
用下面的代碼替換您的for循環。
var responseObj = JSON.parse(JSON.stringify(res));
$.each(responseObj.d, function (index, el) {
console.log("Id:- "+el.id+" Module Id:- "+ el.moduleId
+" Module Name:- "+el.dropdownModuleName);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.