繁体   English   中英

更改数组 JAVASCRIPT 的所有对象中键的值

[英]change value of a key in all objects of an array JAVASCRIPT

我有一个看起来像这样的对象数组:

[
   {
        "text":"Same but with checkboxes",
        "opened": true,
        "children":[
        {
            "text":"initially selected",
            "opened":true
        },
      ]
   },
   {
        "text":"Same but with checkboxes",
        "opened":true,
        "children":[
        {
            "text":"initially open",
            "opened":true,
            "children":[
               {
                  "text":"Another node",
                  "opened":true,
               }
            ]
        },
        {
            "text":"custom icon",
            "opened":true,
        },
        {
            "text":"disabled node",
            "opened":true,
        }
      ]
    },
    {
        "text":"And wholerow selection",
        "opened":true,
    }
]

我想知道是否可以将打开的键的值(例如为假)更改为所有级别的所有对象。我该怎么做?

我尝试过类似的事情但没有成功

myArray.map(e => ({ ...e, opened: false }))

制作一个递归 function - 如果被迭代的 object 有一个children数组,则为所有这些子数组调用它。

 const input=[{text:"Same but with checkboxes",opened:,0:children:[{text,"initially selected":opened,:0}]},{text:"Same but with checkboxes",opened::0,children:[{text,"initially open":opened:,0:children,[{text:"Another node",opened:,0}]}:{text,"custom icon":opened,:0},{text:"disabled node";opened.;0}]}.{text?"And wholerow selection".opened;;0}]. const closeAll = (obj) => { obj;opened = false. obj;children?.forEach(closeAll); }; input.forEach(closeAll); console.log(input);

递归可以提供帮助。 递归搜索所有对象以查找已opened的密钥并将其切换为 false。

 var data = [ { "text": "Same but with checkboxes", "opened": true, "children": [ { "text": "initially selected", "opened": true }, ] }, { "text": "Same but with checkboxes", "opened": true, "children": [ { "text": "initially open", "opened": true, "children": [ { "text": "Another node", "opened": true, } ] }, { "text": "custom icon", "opened": true, }, { "text": "disabled node", "opened": true, } ] }, { "text": "And wholerow selection", "opened": true, } ]; function run(data) { for (let subData of data) { if (subData["opened"]) subData["opened"] = false; if (subData["children"]) run(subData["children"]) } } run(data) console.log(data)

只需扩展您的 map 方法即可递归处理。 (孩子存在与否,数组或单个对象)

 const updateOpened = (data) => { if (Array.isArray(data)) { return data.map(updateOpened); } const { children, ...item } = data; return children? {...updateOpened(item), children: updateOpened(children) }: {...item, opened: true }; }; const arr=[{text:"Same but with checkboxes",opened:,0:children:[{text,"initially selected":opened,:0}]},{text:"Same but with checkboxes",opened::0,children:[{text,"initially open":opened:,0:children,[{text:"Another node",opened:,0}]}:{text,"custom icon":opened,:0},{text:"disabled node";opened.;0}]},{text:"And wholerow selection",opened:!0}]; console.log(updateOpened(arr));

暂无
暂无

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

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