下划线/ lodash合并数组

时间:2015-06-03 10:54:14

标签: javascript arrays underscore.js

你好,有人可以帮忙。

有一些成绩数组,其中包含以下两个类属性:

var arr1 = [{
    "id": 53,
    "name": "Grade 1 AppMonkeyzTest",
    "classes": [{
        "id": 54,
        "name": "Class 1A AppMonkeyzTest"
    }, {
        "id": 55,
        "name": "Class 1B AppMonkeyzTest"
    }, {
        "id": 59,
        "name": "Class BG1 AppMonkeyzTest"
    }]
}, {
    "id": 54,
    "name": "Grade 2 AppMonkeyzTest",
    "classes": [{
        "id": 56,
        "name": "Class AA1 ppMonkeyzTest"
    }, {
        "id": 57,
        "name": "Class BA1 AppMonkeyzTest"
    }]
}];  

var arr2 = [{
    "id": 53,
    "name": "Grade 1 AppMonkeyzTest",
    "classes": [{
        "id": 58,
        "name": "Class BB1 AppMonkeyzTest"
    }]
}];

我想要的只是将它们合并为单个成绩数组,每个年级都有唯一的等级。如下所示:

var merge = [
        {
            "id": 53,
            "name": "Grade 1 AppMonkeyzTest",
            "classes": [{
                "id": 54,
                "name": "Class 1A AppMonkeyzTest"
            }, {
                "id": 55,
                "name": "Class 1B AppMonkeyzTest"
            }, {
                "id": 59,
                "name": "Class BG1 AppMonkeyzTest"
            }, {
                "id": 58,
                "name": "Class BB1 AppMonkeyzTest"
            }]
        },
        {
            "id": 54,
            "name": "Grade 2 AppMonkeyzTest",
            "classes": [{
                "id": 56,
                "name": "Class AA1 ppMonkeyzTest"
            }, {
                "id": 57,
                "name": "Class BA1 AppMonkeyzTest"
            }]
        }];

3 个答案:

答案 0 :(得分:2)

_.merge与回调一起使用

[
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "VisitLondon"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "VisitRotterdam"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "wtf"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "VisitLondon"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "VisitRotterdam"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "wtf"
    },
    {
        "id": 28,
        "settings_name": "QA Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "VisitLondon"
    },
    {
        "id": 28,
        "settings_name": "QA Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "wtf"
    },
    {
        "id": 27,
        "settings_name": "QA Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "VisitLondon"
    },
    {
        "id": 27,
        "settings_name": "QA Default",
        "layer_name": "OpenStreetMapService",
        "case_name": "VisitRotterdam"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "Map2D",
        "case_name": "VisitLondon"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "Map2D",
        "case_name": "VisitRotterdam"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "Map2D",
        "case_name": "wtf"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "Map2D",
        "case_name": "VisitLondon"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "Map2D",
        "case_name": "VisitRotterdam"
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_name": "Map2D",
        "case_name": "wtf"
    },
    {
        "id": 28,
        "settings_name": "QA Default",
        "layer_name": "Map2D",
        "case_name": "VisitLondon"
    },
    {
        "id": 28,
        "settings_name": "QA Default",
        "layer_name": "Map2D",
        "case_name": "wtf"
    },
    {
        "id": 27,
        "settings_name": "QA Default",
        "layer_name": "Map2D",
        "case_name": "VisitLondon"
    },
    {
        "id": 27,
        "settings_name": "QA Default",
        "layer_name": "Map2D",
        "case_name": "VisitRotterdam"
    }
]
[
    {
        "id": 27,
        "settings_name": "QA Default",
        "layer_names": [
            {
                "layer_name": "OpenStreetMapService"
            },
            {
                "layer_name": "Map2D"
            }
        ],
        "case_names": [
            {
                "case_name": "VisitLondon"
            },
            {
                "case_name": "VisitRotterdam"
            }
        ]
    },
    {
        "id": 28,
        "settings_name": "QA Default",
        "layer_names": [
            {
                "layer_name": "OpenStreetMapService"
            },
            {
                "layer_name": "Map2D"
            }
        ],
        "case_names": [
            {
                "case_name": "VisitLondon"
            },
            {
                "case_name": "wtf"
            }
        ]
    },
    {
        "id": 29,
        "settings_name": "Test Default",
        "layer_names": [
            {
                "layer_name": "OpenStreetMapService"
            },
            {
                "layer_name": "Map2D"
            }
        ],
        "case_names": [
            {
                "case_name": "VisitLondon"
            },
            {
                "case_name": "VisitRotterdam"
            },
            {
                "case_name": "wtf"
            }
        ]
    }
]

答案 1 :(得分:1)

在JS中

var arr1 = [{
    "id": 53,
    "name": "Grade 1 AppMonkeyzTest",
    "classes": [{
        "id": 54,
        "name": "Class 1A AppMonkeyzTest"
    }, {
        "id": 55,
        "name": "Class 1B AppMonkeyzTest"
    }, {
        "id": 59,
        "name": "Class BG1 AppMonkeyzTest"
    }]
}, {
    "id": 54,
    "name": "Grade 2 AppMonkeyzTest",
    "classes": [{
        "id": 56,
        "name": "Class AA1 ppMonkeyzTest"
    }, {
        "id": 57,
        "name": "Class BA1 AppMonkeyzTest"
    }]
}];
var arr2 = [{
    "id": 53,
    "name": "Grade 1 AppMonkeyzTest",
    "classes": [{
        "id": 58,
        "name": "Class BB1 AppMonkeyzTest"
    }]
}];
var arr3 = [];
for (var i in arr1) {
    arr3.push(arr1[i]);
    for (var j in arr2) {
        if (arr2[j].id == arr1[i].id) {
            for (var k in arr2[j].classes) {
                arr3[i].classes.push(arr2[j].classes[k]);
            }

        }
    }

}
console.log(arr3);  

DEMO

答案 2 :(得分:0)

您可以使用_.findIndex()

var merge = [];

arr1.forEach(function(object){
    var i = _.findIndex(merge, { id: object.id });
    if(i){
        merge[i].classes = merge[i].classes.concat(object.classes)
    }else{
        merge.push(object);
    }
});

arr2.forEach(function(object){
    var i = _.findIndex(merge, { id: object.id });
    if(i){
        merge[i].classes = merge[i].classes.concat(object.classes)
    }else{
        merge.push(object);
    }
});