0

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

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.