我有多个数组,我想将所有数组的数据合并到一个对象数组中。 所有数据都将在密钥的基础上合并,以便如何制作一个生产级代码,以便合并所有数据。
results[0]=
[ {categoryGroupID: '1231',categoryID: '12311',categoryName: 'External'},
{categoryGroupID: '1231',categoryID: '12312',categoryName: 'Internal'},
{categoryGroupID: '1231',categoryID: '141414141',categoryName: ''},
{categoryGroupID: '1232',categoryID: '12321',categoryName: 'Style'},
{categoryGroupID: '1233',categoryID: '123222',categoryName: ''},
{categoryGroupID: '1233',categoryID: '12331',categoryName: 'Customer Satisfaction'}]
results[1]=
[ { categoryGroupID: '1231',categoryGroupName: 'Store ambience'},
{ categoryGroupID: '1232',categoryGroupName: 'Communication'},
{categoryGroupID: '1233',categoryGroupName: 'Overall Satisfaction'},
{categoryGroupID: '12331',categoryGroupName: null}]
results[2]=
[ {categoryID: '12311',questionID: '12311111',questionName: 'tell me snything'},
{categoryID: '12312',questionID: '12311113',questionName: 'whatever'},
{categoryID: '12311',questionID: '123ques',questionName: 'ashjsh'},
{categoryID: '12311',questionID: '123test',questionName: null}]
results[3]=
[ { questionID: '12311113',choiceID: '1231111111',choiceValue: 'good'},
{ questionID: '12311113',choiceID: '1231111112',choiceValue: 'very good'},
{questionID: '12311113',choiceID: '12311113113',choiceValue: 'bad'}]
我的输出应该是这样的,所以怎么做
"categoryGroup":
[
{"categoryGroupID": "1231","categoryGroupName": "Store ambience",
"category": [
{"categoryID": "12312","categoryName": "Internal",
"question": [
{"questionID": "12311113","questionName": "whatever",
"choice": [
{"choiceID": "1231111111","choiceValue": "good"},
{"choiceID": "1231111112","choiceValue": "very good"},
{"choiceID": "12311113113","choiceValue": "bad",}
],
}]
},
{"categoryID": "12311","categoryName": "External",
"question": [
{"questionID": "12311111","questionName": "tell me snything",},
{"questionID": "123ques","questionName": "ashjsh",},
{"questionID": "123test","questionName": null,}
]},
{"categoryID": "141414141","categoryName": "",}]
},
{"categoryGroupID": "1232","categoryGroupName": "Communication",
"category": [
{"categoryID": "12321","categoryName": "Style"}]
},
{"categoryGroupID": "1233","categoryGroupName": "Overall Satisfaction",
"category": [
{"categoryID": "123222","categoryName": "",},
{"categoryID": "12331","categoryName": "Customer Satisfaction",}]
},
{"categoryGroupID": "12331","categoryGroupName": null}
]
答案 0 :(得分:1)
如果没有进入所有这些的深度以及你将如何手动处理所有情况,这里是一个 如何工作的例子(可能不适用)。
这基于结果的索引(0,1,2,3)将始终相同,并使用filter
和map
Array
函数。对于性能,我不确定它在大型场景中的效果如何。
var results = {};
results[0] =
[{ categoryGroupID: '1231', categoryID: '12311', categoryName: 'External' },
{ categoryGroupID: '1231', categoryID: '12312', categoryName: 'Internal' },
{ categoryGroupID: '1231', categoryID: '141414141', categoryName: '' },
{ categoryGroupID: '1232', categoryID: '12321', categoryName: 'Style' },
{ categoryGroupID: '1233', categoryID: '123222', categoryName: '' },
{ categoryGroupID: '1233', categoryID: '12331', categoryName: 'Customer Satisfaction' }];
results[1] =
[{ categoryGroupID: '1231', categoryGroupName: 'Store ambience' },
{ categoryGroupID: '1232', categoryGroupName: 'Communication' },
{ categoryGroupID: '1233', categoryGroupName: 'Overall Satisfaction' },
{ categoryGroupID: '12331', categoryGroupName: null }];
results[2] =
[{ categoryID: '12311', questionID: '12311111', questionName: 'tell me snything' },
{ categoryID: '12312', questionID: '12311113', questionName: 'whatever' },
{ categoryID: '12311', questionID: '123ques', questionName: 'ashjsh' },
{ categoryID: '12311', questionID: '123test', questionName: null }];
results[3] =
[{ questionID: '12311113', choiceID: '1231111111', choiceValue: 'good' },
{ questionID: '12311113', choiceID: '1231111112', choiceValue: 'very good' },
{ questionID: '12311113', choiceID: '12311113113', choiceValue: 'bad' }];
var o = {
categoryGroup: results[1].map(function (categoryGroup) {
var categoryGroup = {
categoryGroupID: categoryGroup.categoryGroupID,
categoryGroupName: categoryGroup.categoryGroupName
};
categoryGroup['category'] = results[0].filter(function (item) { return item.categoryGroupID == categoryGroup.categoryGroupID })
.map(function (item) {
return {
categoryId: item.categoryID,
categoryName: item.categoryName,
question: results[2].filter(function (ques) { return ques.categoryID == item.categoryID })
.map(function (ques) {
return {
questionId: ques.questionID,
questionName: ques.questionName,
choice: results[3].filter(function (ch) { return ch.questionID == ques.questionID })
.map(function (ch) {
return {
choiceID: ch.choiceID,
choiceValue: ch.choiceValue
}
})
}
})
};
});
return categoryGroup;
})
};
console.log(o);
答案 1 :(得分:-2)
请参阅 - > How to merge two arrays in Javascript and de-duplicate items
var array1 = ["1","2"];
var array2 = ["3", "4"];
var array3 = array1.concat(array2); // Merges both arrays
// [ '1', '2', '3', '4' ]
对于Apache Commons Lang Array Utils,请参阅 - > How can I concatenate two arrays in Java?
js版本为 dojo http://dojotoolkit.org/reference-guide/1.10/dojo/_base/array.html和 util-array https://www.npmjs.com/package/util-array
这是另一个道场示例 http://dojo-toolkit.33424.n3.nabble.com/How-to-concatenate-arrays-in-Dojo-td3744293.html