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

Posted

技术标签:

【中文标题】更改数组 JAVASCRIPT 的所有对象中键的值【英文标题】:change value of a key in all objects of an array 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 ))

【问题讨论】:

【参考方案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));

【讨论】:

以上是关于更改数组 JAVASCRIPT 的所有对象中键的值的主要内容,如果未能解决你的问题,请参考以下文章

如何在Python中更改嵌套字典中键的值[关闭]

更改数组中键的名称

在php中获取多维数组中键的值

如何将数组中键的值添加到字典中? [关闭]

合并一个多维数组中键的值相同的数组

javascript:更改所有键的值顺序