Javascript 在JS中合并数组
假设我有以下数组:Javascript 在JS中合并数组,javascript,arrays,Javascript,Arrays,假设我有以下数组: var first = [ { id: 1, name: 'first' }, { id: 2, name: 'second' }, { id: 3, name: 'third' } ] var second = [ { id: 2, field: 'foo2' }, { id: 3, field: 'foo3' }, { id: 4, field: 'foo4' } ] var third = [ { id: 2,
var first = [
{ id: 1, name: 'first' },
{ id: 2, name: 'second' },
{ id: 3, name: 'third' }
]
var second = [
{ id: 2, field: 'foo2' },
{ id: 3, field: 'foo3' },
{ id: 4, field: 'foo4' }
]
var third = [
{ id: 2, data: 'some2' },
{ id: 5, data: 'some5' },
{ id: 6, data: 'some6' }
]
我希望合并它们以获得以下结果:
var result = [
{ id: 1, name: 'first', field: undefined, data: undefined },
{ id: 2, name: 'second', field: 'foo2', data: 'some2' },
{ id: 3, name: 'third', field: 'foo3', data: undefined },
{ id: 4, name: undefined, field: 'foo4', data: undefined },
{ id: 5, name: undefined, field: undefined, data: 'some5' },
{ id: 6, name: undefined, field: undefined, data: 'some6' }
]
如何使用JavaScript实现这一点?对于您想要的内容,没有简单的解决方案。这是我的建议
var first=[
{id:1,名称:'first'},
{id:2,名称:'second'},
{id:3,名称:'third'}
]
第二个变量=[
{id:2,存档:'foo2'},
{id:3,字段:'foo3'},
{id:4,字段:'foo4'}
];
第三个变量=[
{id:2,数据:'some2'},
{id:4,数据:'some4'},
{id:6,数据:'some6'}
];
var result={};
一、concat(二、三)、forEach(函数(项){
var id=item.id;
变量行=结果[id];
如果(!行){
结果[id]=项目;
返回;
}
for(项目中的var列){
行[列]=项[列];
}
});
var finalResult=Object.keys(result).map(函数id){
返回结果[id];
});
控制台日志(最终结果)代码>您应该获取所有已存在的键,然后使用填充“空”键创建新对象:
解决这个问题的方法可能更短,但这涵盖了所有步骤,包括确保存在未定义的默认属性。它还需要任意数量的输入数组,如果现有对象中的键尚未包含默认键,则可以指定所需的默认键,这样就可以满足您的需要
// merges the key/values of two objects
function merge(a, b) {
var key;
if (a && b) {
for (key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
}
return a;
}
function concatenate() {
var result = [];
var args = arguments[0];
for (var i = 0, l = args.length; i < l; i++) {
result = result.concat(args[i]);
}
return result;
}
// return a default object
function getDefault() {
return {
id: undefined,
name: undefined,
data: undefined,
field: undefined
};
}
// loop over the array and check the id. Add the id as a key to
// a temporary pre-filled default object if the key
// doesn't exist, otherwise merge the existing object and the
// new object
function createMergedArray(result) {
var temp = {};
var out = [];
for (var i = 0, l = result.length; i < l; i++) {
var id = result[i].id;
if (!temp[id]) temp[id] = getDefault();
merge(temp[id], result[i]);
}
// loop over the temporary object pushing the values
// into an output array, and return the array
for (var p in temp) {
out.push(temp[p]);
}
return out;
}
function mergeAll() {
// first concatenate the objects into a single array
// and then return the results of merging that array
return createMergedArray(concatenate(arguments));
}
mergeAll(first, second, third);
//合并两个对象的键/值
函数合并(a,b){
var键;
如果(a&b){
用于(输入b){
如果(b.hasOwnProperty(键)){
a[键]=b[键];
}
}
}
返回a;
}
函数连接(){
var结果=[];
var args=参数[0];
对于(变量i=0,l=args.length;i
小提琴:
函数getByProperty(arr、propName、propValue){
对于(变量i=0;i
可能重复的concat
不起作用。他不仅仅是添加项,他还希望字段的并集。哦,不管是谁把concat
删除了评论。那么,别管我的。@VigneswaranMarimuthu concat不适合我的情况,因为我不想简单地合并数组。我想合并他们的项目这个链接可能会有帮助:它看起来不错,但最终我得到的每个项目只有一个字段,但我想得到每个项目的联合文件。例如,第一个、第二个和第三个数组包含id==2的项,并且我希望在最终结果中得到以下结果:{id:2,名称:'second',字段:'foo2',数据:'some2'}但是我得到了{id:2,名称:'second'}
// merges the key/values of two objects
function merge(a, b) {
var key;
if (a && b) {
for (key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
}
return a;
}
function concatenate() {
var result = [];
var args = arguments[0];
for (var i = 0, l = args.length; i < l; i++) {
result = result.concat(args[i]);
}
return result;
}
// return a default object
function getDefault() {
return {
id: undefined,
name: undefined,
data: undefined,
field: undefined
};
}
// loop over the array and check the id. Add the id as a key to
// a temporary pre-filled default object if the key
// doesn't exist, otherwise merge the existing object and the
// new object
function createMergedArray(result) {
var temp = {};
var out = [];
for (var i = 0, l = result.length; i < l; i++) {
var id = result[i].id;
if (!temp[id]) temp[id] = getDefault();
merge(temp[id], result[i]);
}
// loop over the temporary object pushing the values
// into an output array, and return the array
for (var p in temp) {
out.push(temp[p]);
}
return out;
}
function mergeAll() {
// first concatenate the objects into a single array
// and then return the results of merging that array
return createMergedArray(concatenate(arguments));
}
mergeAll(first, second, third);
function getByProperty(arr, propName, propValue) {
for (var i = 0; i < arr.length; i++) {
if (arr[i][propName] == propValue) return arr[i];
}
}
var limit = first.length + second.length + third.length;
var res = [];
for (var i = 1; i < limit; i++) {
var x = $.extend({}, getByProperty(first, "id", i), getByProperty(second, "id", i), getByProperty(third, "id", i));
console.log(x["id"]);
if (x["id"] === undefined) x["id"] = i;
res.push(x);
}
console.log(res);