将嵌套对象数组转换为另一个嵌套索引对象
Convert an array of nested objects to another nested 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)
您可以将递归调用的结果分配给父级别。
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);
一种简单的递归方法
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}
这是使用 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/object-scan@14.3.1"></script>
免责声明:我是object-scan
的作者
当我有一个类似对象的动态嵌套数组时,我正在处理一个问题。我必须捕获嵌套索引并将其转换为嵌套对象。这是两级嵌套数组的示例,我编写了简单的代码将其转换为嵌套索引对象。我正在寻找迭代或递归方法来处理动态嵌套数组并转换为嵌套的相似索引对象:-
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)
您可以将递归调用的结果分配给父级别。
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);
一种简单的递归方法
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}
这是使用 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/object-scan@14.3.1"></script>
免责声明:我是object-scan
的作者