我想将4个对象数组合并为一个数组
例如: 4个数组,如
var arr1 =[
{ memberID : "81fs", RatingCW:4.5},
{ memberID : "80fs", RatingCW:4},
{ memberID : "82fs", RatingCW:5 },
{ memberID : "83fs", RatingCW:3},
{ memberID : "84fs", RatingCW:4.7}
];
var arr2 =[
{ memberID : "80fs", ratingWW: 4},
{ memberID : "81fs", ratingWW: 4.5},
{ memberID : "83fs", ratingWW: 3},
{ memberID : "82fs", ratingWW: 5},
{ memberID : "84fs", ratingWW: 3.5}
];
var arr3 = [
{ memberID : "80fs", incoCW:4},
{ memberID : "81fs", incoCW:4.5},
{ memberID : "82fs", incoCW:5},
{ memberID : "83fs", incoCW:3},
{ memberID : "84fs", incoCW:4.5}
];
var arr4 = [
{ memberID : "80fs", incoWW:3},
{ memberID : "81fs", incoWW:2.5 },
{ memberID : "82fs", incoWW:5 },
{ memberID : "83fs", incoWW:3 },
{ memberID : "84fs", incoWW:6.5 }
];
和预期数组如:
var finalArr = [
{ memberID : "80fs", RatingCW:4,ratingWW: 4, incoCW:4, incoWW:3},
{ memberID : "81fs", RatingCW:4.5,ratingWW: 4.5, incoCW:4.5, incoWW:2.5 },
{ memberID : "82fs", RatingCW:5,ratingWW: 5, incoCW:5, incoWW:5 },
{ memberID : "83fs", RatingCW:3,ratingWW: 3, incoCW:3, incoWW:3 },
{ memberID : "84fs", RatingCW:4.7,ratingWW: 3.5, incoCW:4.5, incoWW:6.5 }
];
使用lodash或普通javascript进行合并的最佳方式是什么?
答案 0 :(得分:11)
使用lodash,我认为更具可读性。
laravel.log

var arr1 = [{"memberID":"81fs","RatingCW":4.5},{"memberID":"80fs","RatingCW":4},{"memberID":"82fs","RatingCW":5},{"memberID":"83fs","RatingCW":3},{"memberID":"84fs","RatingCW":4.7}],
arr2 = [{"memberID":"80fs","ratingWW":4},{"memberID":"81fs","ratingWW":4.5},{"memberID":"83fs","ratingWW":3},{"memberID":"82fs","ratingWW":5},{"memberID":"84fs","ratingWW":3.5}],
arr3 = [{"memberID":"80fs","incoCW":4},{"memberID":"81fs","incoCW":4.5},{"memberID":"82fs","incoCW":5},{"memberID":"83fs","incoCW":3},{"memberID":"84fs","incoCW":4.5}],
arr4 = [{"memberID":"80fs","incoWW":3},{"memberID":"81fs","incoWW":2.5},{"memberID":"82fs","incoWW":5},{"memberID":"83fs","incoWW":3},{"memberID":"84fs","incoWW":6.5}];
var merged = _(arr1)
.concat(arr2, arr3, arr4)
.groupBy("memberID")
.map(_.spread(_.merge))
.value();
console.log(merged);

以下是codepen:http://codepen.io/kuhnroyal/pen/Wxzdmw
答案 1 :(得分:3)
以下是使用lodash的一些步骤:
var arr1 =[
{ memberID : "81fs", RatingCW:4.5},
{ memberID : "80fs", RatingCW:4},
{ memberID : "82fs", RatingCW:5 },
{ memberID : "83fs", RatingCW:3},
{ memberID : "84fs", RatingCW:4.7}
];
var arr2 =[
{ memberID : "80fs", ratingWW: 4},
{ memberID : "81fs", ratingWW: 4.5},
{ memberID : "83fs", ratingWW: 3},
{ memberID : "82fs", ratingWW: 5},
{ memberID : "84fs", ratingWW: 3.5}
];
var arr3 = [
{ memberID : "80fs", incoCW:4},
{ memberID : "81fs", incoCW:4.5},
{ memberID : "82fs", incoCW:5},
{ memberID : "83fs", incoCW:3},
{ memberID : "84fs", incoCW:4.5}
];
var arr4 = [
{ memberID : "80fs", incoWW:3},
{ memberID : "81fs", incoWW:2.5 },
{ memberID : "82fs", incoWW:5 },
{ memberID : "83fs", incoWW:3 },
{ memberID : "84fs", incoWW:6.5 }
];
var a = _.groupBy(_.flatten([arr1,arr2,arr3,arr4]), 'memberID');
var b = _.map(a, function(val){ return _.merge.apply(_,val) });
console.log(b);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
答案 2 :(得分:1)
您可以使用哈希表来正确引用结果数组。
我也使用未排序的数组,因为对象中有引用memberID
。
var arr1 = [{ memberID: "80fs", RatingCW: 4 }, { memberID: "81fs", RatingCW: 4.5 }, { memberID: "82fs", RatingCW: 5 }, { memberID: "83fs", RatingCW: 3 }, { memberID: "84fs", RatingCW: 4.7 }],
arr2 = [{ memberID: "80fs", ratingWW: 4 }, { memberID: "81fs", ratingWW: 4.5 }, { memberID: "82fs", ratingWW: 5 }, { memberID: "83fs", ratingWW: 3 }, { memberID: "84fs", ratingWW: 3.5 }],
arr3 = [{ memberID: "80fs", incoCW: 4 }, { memberID: "81fs", incoCW: 4.5 }, { memberID: "82fs", incoCW: 5 }, { memberID: "83fs", incoCW: 3 }, { memberID: "84fs", incoCW: 4.5 }],
arr4 = [{ memberID: "80fs", incoWW: 3 }, { memberID: "81fs", incoWW: 2.5 }, { memberID: "82fs", incoWW: 5 }, { memberID: "83fs", incoWW: 3 }, { memberID: "84fs", incoWW: 6.5 }],
merged = [];
[arr1, arr2, arr3, arr4].forEach(function (a) {
a.forEach(function (b) {
if (!this[b.memberID]) {
this[b.memberID] = {};
merged.push(this[b.memberID]);
}
Object.keys(b).forEach(function (k) {
this[b.memberID][k] = b[k];
}, this);
}, this);
}, Object.create(null));
console.log(merged);
ES6表示未分类数据。
var arr1 = [{ memberID: "80fs", RatingCW: 4 }, { memberID: "81fs", RatingCW: 4.5 }, { memberID: "82fs", RatingCW: 5 }, { memberID: "83fs", RatingCW: 3 }, { memberID: "84fs", RatingCW: 4.7 }],
arr2 = [{ memberID: "80fs", ratingWW: 4 }, { memberID: "81fs", ratingWW: 4.5 }, { memberID: "82fs", ratingWW: 5 }, { memberID: "83fs", ratingWW: 3 }, { memberID: "84fs", ratingWW: 3.5 }],
arr3 = [{ memberID: "80fs", incoCW: 4 }, { memberID: "81fs", incoCW: 4.5 }, { memberID: "82fs", incoCW: 5 }, { memberID: "83fs", incoCW: 3 }, { memberID: "84fs", incoCW: 4.5 }],
arr4 = [{ memberID: "80fs", incoWW: 3 }, { memberID: "81fs", incoWW: 2.5 }, { memberID: "82fs", incoWW: 5 }, { memberID: "83fs", incoWW: 3 }, { memberID: "84fs", incoWW: 6.5 }],
merged = [];
[arr1, arr2, arr3, arr4].forEach((hash => a => a.forEach(b => {
if (!hash[b.memberID]) {
hash[b.memberID] = {};
merged.push(hash[b.memberID]);
}
Object.assign(hash[b.memberID], b);
}))(Object.create(null)));
console.log(merged);
答案 3 :(得分:1)
arr1.map(a =>
Object.assign({},a,
arr2.find(b => b.memberID === a.memberID),
arr3.find(c => c.memberID === a.memberID),
arr4.find(d => d.memberID === a.memberID)
)
)
这是最简单的代码...这基本上是一个单一解决方案。 性能可能不是最好的,但是对于较小的数据集来说效果很好。
答案 4 :(得分:0)
纯粹的JS方式,这很艰难,花了几个小时才能得到它。
var arr1 = [{"memberID":"81fs","RatingCW":4.5},{"memberID":"80fs","RatingCW":4},{"memberID":"82fs","RatingCW":5},{"memberID":"83fs","RatingCW":3},{"memberID":"84fs","RatingCW":4.7}],
arr2 = [{"memberID":"80fs","ratingWW":4},{"memberID":"81fs","ratingWW":4.5},{"memberID":"83fs","ratingWW":3},{"memberID":"82fs","ratingWW":5},{"memberID":"84fs","ratingWW":3.5}],
arr3 = [{"memberID":"80fs","incoCW":4},{"memberID":"81fs","incoCW":4.5},{"memberID":"82fs","incoCW":5},{"memberID":"83fs","incoCW":3},{"memberID":"84fs","incoCW":4.5}],
arr4 = [{"memberID":"80fs","incoWW":3},{"memberID":"81fs","incoWW":2.5},{"memberID":"82fs","incoWW":5},{"memberID":"83fs","incoWW":3},{"memberID":"84fs","incoWW":6.5}];
const arrs=[...arr1,...arr2,...arr3,...arr4];
const noDuplicate=arr=>[...new Set(arr)]
const allIds=arrs.map(ele=>ele.memberID);
const ids=noDuplicate(allIds);
const result=ids.map(id=>
arrs.reduce((self,item)=>{
return item.memberID===id?
{...self,...item} : self
},{})
)
console.log(result);