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)
-
You have no JSON in your question, and there is no such thing as a JSON Object. Just arrays and objects.Heretic Monkey– Heretic Monkey2021年05月04日 20:34:27 +00:00Commented May 4, 2021 at 20:34
-
Seems like you probably need to write a recursive function.Barmar– Barmar2021年05月04日 20:35:02 +00:00Commented May 4, 2021 at 20:35
-
Can you give an example of the result obj would look like?Moti Korets– Moti Korets2021年05月04日 20:36:02 +00:00Commented 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:{}}}C. T.– C. T.2021年05月04日 20:38:42 +00:00Commented May 4, 2021 at 20:38
-
it will be more understandable if you add the desired output to the questionamir– amir2021年05月04日 20:40:48 +00:00Commented May 4, 2021 at 20:40
3 Answers 3
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}
2 Comments
({ ...undefined }) is valid but [ ...undefined ] is not[...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.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
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
Explore related questions
See similar questions with these tags.