【问题标题】:change value of a key in all objects of an array JAVASCRIPT更改数组 JAVASCRIPT 的所有对象中键的值
【发布时间】:2022-01-18 23:26:24
【问题描述】:

我有一个如下所示的对象数组:

[
   {
        "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,
    }
]

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

我尝试了类似的方法但没有成功

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

【问题讨论】:

    标签: javascript arrays json object ecmascript-6


    【解决方案1】:

    创建一个递归函数 - 如果被迭代的对象有一个 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);

    【讨论】:

      【解决方案2】:

      递归可以提供帮助。递归搜索所有对象的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)

      【讨论】:

        【解决方案3】:

        只需扩展您的 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));

        【讨论】:

          猜你喜欢
          • 2021-02-12
          • 2021-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多