FeRcHo
FeRcHo

Reputation: 1151

change value of a key in all objects of an array JAVASCRIPT

I have an array of objects that looks like this:

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

I want to know if it is possible to change the value for example of the key opened (to false) to all objects at all levels .. how can I do this?

I tried something like that without success

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

Upvotes: 0

Views: 311

Answers (3)

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

Just extend your map method to handle recursively. (children exist or not and Array or single object)

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));

Upvotes: 0

Deepak
Deepak

Reputation: 2742

Recursion is here to help. Recursively search all the objects for opened key and switch it to 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)

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370659

Make a recursive function - if the object being iterated over has a children array, call it for all such 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);

Upvotes: 2

Related Questions