简体   繁体   English

如何将“键”:“值”对添加到“值”在不同数组上迭代的嵌套对象数组中?

[英]How to add a 'key': 'value' pair to a nested array of objects where the 'value' iterates over a different array?

I have an array of objects like so where there can be more or less items:我有一个像这样的对象数组,其中可以有更多或更少的项目:

arrOfItems = [
    {
        "type": "apple",
        "earth: "blue"
    }
    {
        "type": "orange",
        "attrs": {
            "id": 21,
            "data": "RET",
            "data2": null,
            "order": null,
            "order2": null,
            "type": "property",
            "char": "@"
        }
    },
    {
        "type": "apple",
        "earth: "green"
    },
    {
        "type": "apple",
        "earth: "yellow"
    },
    {
        "type": "orange",
        "attrs": {
            "id": 28,
            "data": "EMC",
            "data2": null,
            "order": null,
            "order2": null,
            "type": "property",
            "char": "@"
        }
    },
]

Each item inside the array has a structure.数组内的每一项都有一个结构。 So if "type" = "apple" then another key will be added "earth".因此,如果“type”=“apple”,则将添加另一个键“earth”。 If "type" = "orange", another key "attrs" will be added which is in the following structure:如果“type”=“orange”,则会添加另一个键“attrs”,其结构如下:

"attrs": {
    "id":,
    "data": "",
    "data2": null,
    "order": null,
    "order2": null,
    "type": "",
    "char": ""
}

For each item with type: "orange", how can I add a key 'class' to the attrs object where that key is assigned to an item from the following array:对于类型为“orange”的每个项目,我如何向 attrs 对象添加一个键“类”,其中该键被分配给以下数组中的一个项目:

arrOfProps = ["prop_1", "prop_2", "prop_3", "prop_4" "prop_5", "prop_6"]

For example, based on the arrOfItems above, how do I get to the following:例如,基于上面的 arrOfItems,我如何获得以下内容:

arrOfItems = [
    {
        "type": "apple",
        "earth: "blue"
    }
    {
        "type": "orange",
        "attrs": {
            "class": "prop_1",
            "id": 21,
            "data": "RET",
            "data2": null,
            "order": null,
            "order2": null,
            "type": "property",
            "char": "@"
        }
    },
    {
        "type": "apple",
        "earth: "green"
    },
    {
        "type": "apple",
        "earth: "yellow"
    },
    {
        "type": "orange",
        "attrs": {
            "class": "prop_2",
            "id": 28,
            "data": "EMC",
            "data2": null,
            "order": null,
            "order2": null,
            "type": "property",
            "char": "@"
        }
    },
    ......... For the next item with "type": "orange", we have "class": "prop_3", etc.
]

So far, I have:到目前为止,我有:

arrOfItems.forEach( element =>
    if(element.type === "orange") {
        obj.attrs["class"] = arrOfProps[??];
    }
)

However I'm not sure how to go further.但是我不确定如何更进一步。 Would anyone have any idea?有人会有什么想法吗?

You can simply iterate over this while keeping track of the assigned props:你可以简单地迭代这个,同时跟踪分配的道具:

 const arrOfItems = [ { type: 'apple', earth: 'blue' }, { "type": "orange", "attrs": { "id": 21, "data": "RET", "data2": null, "order": null, "order2": null, "type": "property", "char": "@" } }, { type: 'apple', earth: 'green' }, { type: 'apple', earth: 'yellow' }, { "type": "orange", "attrs": { "id": 28, "data": "EMC", "data2": null, "order": null, "order2": null, "type": "property", "char": "@" } }, ] const arrOfProps = ["prop_1", "prop_2", "prop_3", "prop_4", "prop_5", "prop_6"]; let nextProp = 0; for (const element of arrOfItems) { if (element.type !== 'orange') continue; element.attrs.class = arrOfProps[nextProp++]; } console.log(arrOfItems);

Of course, if you have more oranges than elements in arrOfProps , then class will be assigned to undefined .当然,如果你的橙子比arrOfProps元素arrOfProps ,那么class将被分配给undefined

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

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