1

I'm currently working with an array of javascript objects with X-amount of array's with the same type of object inside it, in a specific property (in this case, 'modelChildren')

I would like to flatten this into just one array of objects, and return the level at which it was found. The solution does not have to be plain javascript, as I use lodash for many situations. Ideally I would also like to remove the 'modelChildren' field once finished.

Any help would be appreciated. Thanks!

input:

[{
  id: 1,
  name: foo
  modelChildren: [
   {
    id: 2,
    name: bar,
    modelChildren: [
      {
      id: 3,
      name: foobar
      },
      {
      id: 4,
      name: foobarfoo
      }
    ] 
   }
  ] 
}]

expected result:

[{
   id: 1,
   name: foo,
   level: 1
 {
   id: 2,
   name: bar,
   level: 2
 },
 {
   id: 3,
   name: foobar,
   level: 3
 },
 {
   id: 4,
   name: foobarfoo
   level: 3
 }]

1 Answer 1

1

This can be quite easy, it is just Tree Traversal

So you just need to traverse it and remember the level, while storing "nodes" when you are in them.

For example this code

const source = [{
    id: 1,
    name: 'foo',
    modelChildren: [
        {
            id: 2,
            name: 'bar',
            modelChildren: [
                {
                    id: 3,
                    name: 'foobar'
                },
                {
                    id: 4,
                    name: 'foobarfoo'
                }
            ]
        }
    ],
}, {
    id: 5,
    name: 'foo',
    modelChildren: [
        {
            id: 6,
            name: 'bar',
            modelChildren: [
                {
                    id: 7,
                    name: 'foobar'
                },
                {
                    id: 8,
                    name: 'foobarfoo'
                }
            ]
        },
        {
            id: 9,
            name: 'bar',
            modelChildren: [
                {
                    id: 10,
                    name: 'foobar'
                },
                {
                    id: 11,
                    name: 'foobarfoo'
                }
            ]
        }
    ],
}
];
const newSource = [];
const _ = require('lodash');

function doIt(items, level) {
    if (!items) {
        return;
    }

    items.forEach(item => {
        newSource.push(_.merge({level}, _.pick(item, ['id', 'name'])));
        doIt(item.modelChildren, level + 1);
    })
}

doIt(source, 1);
console.log(newSource);

Having this output

[ { level: 1, id: 1, name: 'foo' },
  { level: 2, id: 2, name: 'bar' },
  { level: 3, id: 3, name: 'foobar' },
  { level: 3, id: 4, name: 'foobarfoo' },
  { level: 1, id: 5, name: 'foo' },
  { level: 2, id: 6, name: 'bar' },
  { level: 3, id: 7, name: 'foobar' },
  { level: 3, id: 8, name: 'foobarfoo' },
  { level: 2, id: 9, name: 'bar' },
  { level: 3, id: 10, name: 'foobar' },
  { level: 3, id: 11, name: 'foobarfoo' } ]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.