1

I am working on one problem when I have one dynamic nested array of similar kind of objects. I have to capture the nested indexes convert it to nested object. Here is the example of two level nested array and I have written simple code to convert it to nested index object. I am looking for iterative or recursive method to handle dynamic nested array and convert to nested similar index object:-

var arr= [
  {
    "index": "1",
    "subRows": [
      {
        "index": "2",
        "subRows": undefined
      },
      {
        "index": "3",
        "subRows": undefined
      }
    ]
  },
  {
    "index": "4",
    "subRows": [
      {
        "index": "5",
        "subRows": undefined
      }
    ]
  }
];

var obj={}
for(var i =0; i<arr.length; i++) {
  obj[i]={};
  if(arr[i].subRows) {
     for(var j=0; j<arr[i].subRows.length; j++) {
       obj[i][j] = {};
     }
  }
}

console.log(obj)

7
  • You have no JSON in your question, and there is no such thing as a JSON Object. Just arrays and objects. Commented May 4, 2021 at 20:34
  • Seems like you probably need to write a recursive function. Commented May 4, 2021 at 20:35
  • Can you give an example of the result obj would look like? Commented May 4, 2021 at 20:36
  • @MotiKorets the out put will be like this, you can also run this code.0: {0:{0:{},1:{}}, 1:{0:{}}} Commented May 4, 2021 at 20:38
  • it will be more understandable if you add the desired output to the question Commented May 4, 2021 at 20:40

3 Answers 3

4

one simple recursive approach

var arr = [{
    "index": "1",
    "subRows": [{
        "index": "2",
        "subRows": undefined
      },
      {
        "index": "3",
        "subRows": undefined
      }
    ]
  },
  {
    "index": "4",
    "subRows": [{
      "index": "5",
      "subRows": undefined
    }]
  }
];


let obj = {
  ...arr.map(function convert(obj) {
    return { 
      ...obj.subRows?.map(convert)
    }
  })
};

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

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

2 Comments

TIL ({ ...undefined }) is valid but [ ...undefined ] is not
@Thankyou: it would be convenient if [...undefined] acted like [...([])], but I think I understand why it's not done that way... a question of parametricity, one that doesn't apply in the Object case.
2

You could just assign the result of the recursive call to the parent level.

const
    convert = array => array.reduce((o, { subRows = [] }, index) => {
        o[index] = convert(subRows);
        return o;
    }, {}),
    array = [{ index: "1", subRows: [{ index: "2", subRows: undefined }, { index: "3", subRows: undefined }] },{ index: "4", subRows: [{ index: "5", subRows: undefined }] }],
    result = convert(array);

console.log(result);

4 Comments

I don't want to use that index, but actual index.
This looks great. Thanks
please try it. it should work, because it works for the second level without specifying.
I verified, it works for any nesting level. Thanks
0

Here is an iterative solution using object-scan.

// const objectScan = require('object-scan');

const myArr = [{ index: '1', subRows: [{ index: '2', subRows: undefined }, { index: '3', subRows: undefined }] }, { index: '4', subRows: [{ index: '5', subRows: undefined }] }];

const extract = (arr) => objectScan(['**(^subRows$)'], {
  reverse: false,
  useArraySelector: false,
  breakFn: ({ key, property, context }) => {
    if (!Number.isInteger(property)) {
      return;
    }
    context.length = Math.ceil(key.length / 2);
    const last = context[context.length - 1];
    last[property] = {};
    context.push(last[property]);
  }
})(arr, [{}])[0];

console.log(extract(myArr));
// => { '0': { '0': {}, '1': {} }, '1': { '0': {} } }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>

Disclaimer: I'm the author of object-scan

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.