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

Posted

技术标签:

【中文标题】如何在 NodeJS 中展开对象数组?【英文标题】:How to unwind an Array of Objects in NodeJS? 【发布时间】:2016-11-24 15:38:12 【问题描述】:

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

这是我的示例数据

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】:

你试过递归吗?

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));

【讨论】:

太棒了,这就是我想要的。我曾尝试过递归,但不知何故弄乱了我的逻辑。

以上是关于如何在 NodeJS 中展开对象数组?的主要内容,如果未能解决你的问题,请参考以下文章

如何将菊花链/点符号中的 JavaScript 对象展开为具有嵌套对象和数组的对象?

如何在NodeJS服务器中解析ajax发送的数组/对象

NodeJS Cluster如何在工作人员之间共享对象数组

在 NodeJS 中解析 JSON 对象数组

如何在Nodejs中将json对象数组转换为String?

如何更新数组NodeJS,MongoDB中的特定对象