如何在密钥内获取childData

时间:2018-09-24 09:08:04

标签: javascript arrays ajax for-loop

我创建了一个脚本,在其中我根据数据的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中获取dropdownModuleNamemoduleId等。所以我要做的就是循环结果。发生了什么事,但是我的控制台上没有显示任何结果...

这是我的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循环内的控制台未返回结果

4 个答案:

答案 0 :(得分:0)

如果您收到上面的回复,则可能是问题所在

dim nos() as integer={1,2,3,4}
dim names() as string = {"a","b","c","d"}
for i = 0 to 3
     array.sort(names &"  "&nos)
next
console.readKey

res是基于花括号的对象。 id成为res的属性,因此对其进行访问将是res.1,但数字不符合javascript标识符/变量名。

尝试为响应添加另一个级别,但使用其他密钥吗?

EDIT:或其他方法,例如遍历属性 例如

res[i]

答案 1 :(得分:0)

用这样的东西替换循环

var item = vPag.DropIntoList(itemMaster, shpList, 1);
var target = vPag.DrawRectangle(4, 9, 6, 7.5);
item.AutoConnect(target, Visio.VisAutoConnectDir.visAutoConnectDirNone);

for (var i in res) {
  console.log(res[i]);
}

答案 2 :(得分:0)

您需要使用嵌套循环来查找子值...在下面的示例中,我有$.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>

答案 3 :(得分:0)

尝试以下代码可能会在您的控制台中产生结果。

用下面的代码替换您的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);
});