2


i need some help at looping over an Object with Array of Objects

Here is an example Object:

const node = {
    documents: [{
        name: "test1"
    }],
    children: [{
        documents: [{
            name: "test2"
        }],
        children: [{
            documents: [{
                name: "test3"
            }],
            children: []
        }]
    }]
};


My Problem
I tried some of the solutions from here but it's not quite what I need. Basically i want to move every object from the documents Array into the childrens Array.
I already got that but currently its only doing it in the firts Object so not recursive...
I cant figure it out without creating an Frankenstein-ish code...
I tried like i mentioned before i used

Array.concat()

to append documents to children but only in the first Object.
What it should look like

const node = {
    children: [
    {
    name: "test1"
    },
    {
    children: [
        {
      name: "test2"
      },
      {
      children: [
        {name: "test3"}
      ]
      }
    ]
    }
  ]
};

Now in every "depth" the documents were "appended" to children.
And that should happen for every children key (depth).

Is someone kind enough to help me out?


EDIT:

I guess my example is not good enough. Here is some actual data:

      const node = {
        id: 1,
        name: "Root Node",
        documents: [
          { name: "Doc Root 1", id: 1234 },
          { name: "Doc Root 2", id: 1235 }
        ],
        children: [
          {
            id: 2,
            name: "Sub Node Node1",
            documents: [
              { name: "Doc SubNote 1 1", id: 1236 },
              { name: "Doc SubNote 1 2", id: 1237 }
            ],
            children: [{
              id: 3,
              name: "Sub Sub Node Node 1",
              documents: [
                { name: "Doc SubSubNote 1 1", id: 1238 },
                { name: "Doc SubSubNote 1 2", id: 1239 }
              ],
              children: null,
              addTreeNodeDisabled: true,
              addLeafNodeDisabled: true,
              editNodeDisabled: true,
              delNodeDisabled: true,
            }],
            addTreeNodeDisabled: true,
            addLeafNodeDisabled: true,
            editNodeDisabled: true,
            delNodeDisabled: true,
          }
        ],
        addTreeNodeDisabled: true,
        addLeafNodeDisabled: true,
        editNodeDisabled: true,
        delNodeDisabled: true,
      };
4
  • do documents can have childrens? Commented Jan 6, 2020 at 13:50
  • no documents is always an array with objects Commented Jan 6, 2020 at 13:52
  • the data structure is quite mixed up. please add the wanted result of it. Commented Jan 6, 2020 at 14:43
  • Sorry for that, I messed up the export :( Now its a better example Commented Jan 7, 2020 at 7:20

2 Answers 2

3

You could concat a copy of documents with the childrens by calling the function again.

var f = ({ documents, children, ...o }) => {
        children = [...(documents || []), ...(children || [])].map(f);
        return children.length
            ? { ...o, children }
            : o;
    },
    data = { id: 1, name: "Root Node", documents: [{ name: "Doc Root 1", id: 1234 }, { name: "Doc Root 2", id: 1235 }], children: [{ id: 2, name: "Sub Node Node1", documents: [{ name: "Doc SubNote 1 1", id: 1236 }, { name: "Doc SubNote 1 2", id: 1237 }], children: [{ id: 3, name: "Sub Sub Node Node 1", documents: [{ name: "Doc SubSubNote 1 1", id: 1238 }, { name: "Doc SubSubNote 1 2", id: 1239 }], children: null, addTreeNodeDisabled: true, addLeafNodeDisabled: true, editNodeDisabled: true, delNodeDisabled: true, }], addTreeNodeDisabled: true, addLeafNodeDisabled: true, editNodeDisabled: true, delNodeDisabled: true, }], addTreeNodeDisabled: true, addLeafNodeDisabled: true, editNodeDisabled: true, delNodeDisabled: true },
    result = f(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

That is very good but I have an error. in some cases children can be null and it throws an length is undefined error.
add (children && children .length) instead of children .length
Can you have a look at my Edit? I cant get your code to run with this data. It runs, but is loses some keys :(
2

I've used recursion here to achieve the results.

const node = {"documents":[{"name":"test1"}],"children":[{"documents":[{"name":"test2"}],"children":[{"documents":[{"name":"test3"}],"children":[]}]}]}

const unwrap = (v) => ({...{...v}[0]});

const merge = (node) => ([...node.documents, {children: (unwrap(node.children).hasOwnProperty("children") ? merge(unwrap(node.children)) : node.children) }]);

console.log(merge(node));

2 Comments

Hi, maybe you can try with the data from my edit. I cant get yours to work :(
the datasets are nowhere near the same, i suggest you add a different question for that, and add a smaller object with clear and exact data structure.

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.