如何在NodeJS中展开对象数组?

时间:2016-07-21 07:20:41

标签: javascript arrays node.js multidimensional-array

我想要展开一组嵌套了对象数组的对象。嵌套级别未定义,并且在整个阵列中不一致。

这是我的示例数据

var data = [{
    id: 1,
    name: 'Harshal',
    subjects: [{
        id: 1,
        name: 'English',
        chapters: [{
            id: 1,
            name: 'Grammar'
        }, {
            id: 2,
            name: 'Comprehension'
        }]
    }, {
        id: 2,
        name: 'Maths',
        chapters: [{
            id: 1,
            name: 'Algebra'
        }, {
            id: 2,
            name: 'Geometry'
        }]
    }]
}, {
    id: 2,
    name: 'Pankaj',
    subjects: [{
        id: 3,
        name: 'Marathi',
        chapters: [{
            id: 1,
            name: 'Kavita',
            topics: [{
                id: 1,
                name: 'Topic 1'
            }]
        }]
    }, {
        id: 4,
        name: 'Hindi',
        chapters: [{
            id: 1,
            name: 'Katha',
            topics: [{
                id: 2,
                name: 'Topic 2'
            }, {
                id: 3,
                name: 'Topic 3'
            }]
        }]
    }]
}];

我想获得这样的输出:

var op = [{
    id: 1,
    name: 'Harshal',
    subjects: {
        id: 1,
        name: 'English',
        chapters: {
            id: 1,
            name: 'Grammar'
        }
    }
}, {
    id: 1,
    name: 'Harshal',
    subjects: {
        id: 1,
        name: 'English',
        chapters: {
            id: 2,
            name: 'Comprehension'
        }
    }
}, {
    id: 1,
    name: 'Harshal',
    subjects: {
        id: 2,
        name: 'Maths',
        chapters: {
            id: 1,
            name: 'Algebra'
        }
    }
}, {
    id: 1,
    name: 'Harshal',
    subjects: {
        id: 2,
        name: 'Maths',
        chapters: {
            id: 2,
            name: 'Geometry'
        }
    }
}, {
    id: 2,
    name: 'Pankaj',
    subjects: {
        id: 3,
        name: 'Marathi',
        chapters: {
            id: 1,
            name: 'Kavita',
            topics: {
                id: 1,
                name: 'Topic 1'
            }
        }
    }
}, {
    id: 2,
    name: 'Pankaj',
    subjects: {
        id: 4,
        name: 'Hindi',
        chapters: {
            id: 1,
            name: 'Katha',
            topics: {
                id: 2,
                name: 'Topic 2'
            }
        }
    }
}, {
    id: 2,
    name: 'Pankaj',
    subjects: {
        id: 4,
        name: 'Hindi',
        chapters: {
            id: 1,
            name: 'Katha',
            topics: {
                id: 3,
                name: 'Topic 3'
            }
        }
    }
}];

我曾尝试与pull-unwind合作,但我想其中存在一些问题。如果有人有任何其他想法,我愿意实施这些想法。

1 个答案:

答案 0 :(得分:0)

你试过递归吗?



var data = [{
    id: 1,
    name: 'Harshal',
    subjects: [{
        id: 1,
        name: 'English',
        chapters: [{
            id: 1,
            name: 'Grammar'
        }, {
            id: 2,
            name: 'Comprehension'
        }]
    }, {
        id: 2,
        name: 'Maths',
        chapters: [{
            id: 1,
            name: 'Algebra'
        }, {
            id: 2,
            name: 'Geometry'
        }]
    }]
}, {
    id: 2,
    name: 'Pankaj',
    subjects: [{
        id: 3,
        name: 'Marathi',
        chapters: [{
            id: 1,
            name: 'Kavita',
            topics: [{
                id: 1,
                name: 'Topic 1'
            }]
        }]
    }, {
        id: 4,
        name: 'Hindi',
        chapters: [{
            id: 1,
            name: 'Katha',
            topics: [{
                id: 2,
                name: 'Topic 2'
            }, {
                id: 3,
                name: 'Topic 3'
            }]
        }]
    }]
}];

function unravel(obj)
{
    var out = [];
    var added = false;
    for(var i in obj) {
	if(obj[i] instanceof Array) {
	    for(var j in obj[i]) {
		var r = unravel(obj[i][j]);
		for(var k in r) {
		    var a = {};
		    for(var key in obj) { // make copy of obj
			a[key] = obj[key];
		    }
		    a[i] = r[k];
		    added = true;
		    out.push(a);
		}
	    }
	}
    }
    if(!added) {
	out.push(obj);
    }
    return out;
}

var op = [];
for(var i in data)
    op = op.concat(unravel(data[i]));
console.log(JSON.stringify(op, null, 4));