简体   繁体   English

javascript:遍历嵌套对象/数组并添加属性

[英]javascript: looping through nested object/array and adding property

I am not very strong with Javascript. 我对Java语言不是很坚强。 I have a nested array which is a JSON representation of the backend data. 我有一个嵌套数组,它是后端数据的JSON表示形式。 It shows a list of proofs and the images used in each proof. 它显示了样张列表以及每个样张中使用的图像。 Its looks like below: 其外观如下:

var project = [{
    "proof":"Proof_1",
    "images":[
        {
            "image_id":"12469",
            "name":"1911791794.jpg",
        },
        {
            "image_id":"12470",
            "name":"1911802897.jpg"
        },
        {
            "image_id":"12471",
            "name":"1911761073.jpg"
        }
},
{
    "proof":"Proof_2",
    "images":[
        {
            "image_id":"12469",
            "name":"1911791794.jpg",
        },
        {
            "image_id":"12470",
            "name":"1911802897.jpg"
        }
}];

I want to add the image_count to each proof section,so that modified data structure looks like this: 我想将image_count添加到每个证明部分,以便修改后的数据结构如下所示:

var project = [{
    "proof":"Proof_1",
    "image_count": 3, //<----this is new property I want to add
    "images":[
        {
            "image_id":"12469",
            ...

I checked some answers but because of my lack of understanding javascript iteration properly I am unable to get this done. 我检查了一些答案,但由于缺乏对javascript迭代的正确理解,因此无法完成此工作。

When I do: for (var proof in project) { console.log(proof); 当我这样做时:for(项目中的无证明){console.log(proof); } }

I just get 0,1,2 ...etc printed. 我只得到0,1,2 ...等打印。 I am not getting this, so I help someone in SO will help me understand how to add this property I want. 我没有得到这个,所以我帮助SO中的某人将帮助我了解如何添加所需的此属性。

Thanks in advance. 提前致谢。

You can take advantage of Array.prototype.map method: 您可以利用Array.prototype.map方法:

project = project.map(function (item) {
    item.image_count = item.images.length;
    return item;
});

Working demo. 工作演示。

Also, as @Sebastian Lasse pointed out - you should name your array using plural form to avoid confusion ( projects instead of project ). 另外,正如@Sebastian Lasse指出的那样-您应使用复数形式命名数组,以避免混淆( projects而不是project )。

You can use .map or simple loop 您可以使用.map或简单循环

 var projects = [{ "proof": "Proof_1", "images": [{ "image_id": "12469", "name": "1911791794.jpg", }, { "image_id": "12470", "name": "1911802897.jpg" }, { "image_id": "12471", "name": "1911761073.jpg" }] }, { "proof": "Proof_2", "images": [{ "image_id": "12469", "name": "1911791794.jpg", }, { "image_id": "12470", "name": "1911802897.jpg" }] }]; projects = projects.map(function (element) { element.image_count = element.images.length; return element; }); console.log(projects); var len = projects.length, i; for (i = 0; i < len; i++) { projects[i].image_count = projects[i].images.length; } console.log(projects); 

You could - after correcting the missing ] error in your JSON - do this : 您可以-纠正JSON中丢失的[ ]错误后,执行以下操作:

project.forEach(function(proof) {
    proof.image_count = proof.images.length;
})

demo -> http://jsfiddle.net/dLd8wvpb/ 演示-> http://jsfiddle.net/dLd8wvpb/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM