array2
是匹配类似用户的游戏的对象集合。 (类似的用户是共同利益的用户)
问题:这是可能的,在我的情况下,发生了两个相同的游戏 - 数组1中的游戏也在数组2中。在第一个数组中,游戏在那里,因为它与用户的兴趣相匹配。在第二个数组中,游戏之所以存在,因为类似的用户已经购买了该游戏。 问题:underscore.js具有一个不错的小函数联合()http://underscorejs.org/#union
,它为您提供了两个数组的结合,但它与一系列对象不起作用,仅适用于原始值。我该如何工作给我一系列对象?
您可以轻松地实施自己的。在这种情况下,我们制作函数通用,以便它可以使用任何数据类型的数组并使用提供的比较器函数。
// arr1 and arr2 are arrays of any length; equalityFunc is a function which
// can compare two items and return true if they're equal and false otherwise
function arrayUnion(arr1, arr2, equalityFunc) {
var union = arr1.concat(arr2);
for (var i = 0; i < union.length; i++) {
for (var j = i+1; j < union.length; j++) {
if (equalityFunc(union[i], union[j])) {
union.splice(j, 1);
j--;
}
}
}
return union;
}
function areGamesEqual(g1, g2) {
return g1.title === g2.title;
}
// Function call example
arrayUnion(arr1, arr2, areGamesEqual);
您可以使用下划线的方式来做到这一点:
// collectionUnion(*arrays, iteratee)
function collectionUnion() {
var args = Array.prototype.slice.call(arguments);
var it = args.pop();
return _.uniq(_.flatten(args, true), it);
}
它只是对原始函数的改进,添加了一个iTeretree到工作集合(对象的数组)。
在这里如何使用它:
_.union(*arrays)
(ES6/ES2015)将为您提供帮助。
// collectionUnion(*arrays, iteratee)
function collectionUnion() {
var args = Array.prototype.slice.call(arguments);
var it = args.pop();
return _.uniq(_.flatten(args, true), it);
}
var a = [{id: 0}, {id: 1}, {id: 2}];
var b = [{id: 2}, {id: 3}];
var c = [{id: 0}, {id: 1}, {id: 2}];
var result = collectionUnion(a, b, c, function (item) {
return item.id;
});
console.log(result); // [ { id: 0 }, { id: 1 }, { id: 2 }, { id: 3 } ]
使用SET函数在打字稿中确实很容易。
const info1 = {id: 1}
const info2 = {id: 2}
const info3 = {id: 3}
const array1 = [info1, info2]
const array2 = [info1, info3]
const union = [...new Set([...array1, ...array2])]
console.log(union)
jquery具有方法
const firstArr = [
{
id: '1',
winner: 'Captain America',
},
{
id: '4',
winner: 'Aquaman',
},
{
id: '2',
winner: 'Captain America',
},
{
id: '3',
winner: 'Aquaman',
},
]
const secondArr = [
{
id: '1',
winner: 'Wonder women',
},
{
id: '2',
winner: 'Param',
strenths: ['fly', 'fight', 'speed'],
},
]
const mergedArray = [...secondArr, ...firstArr]
let set = new Set()
let unionArray = mergedArray.filter((item) => {
if (!set.has(item.id)) {
set.add(item.id)
return true
}
return false
}, set)
console.log(unionArray);
:
extend
$.extend(true, object1, object2);
如果列表可以为空,请确保将一个空对象附加到它。
var objects = [{ bar : 1, nuts : 2} , {foo : 3, nuts : 4}]
_.extend.apply(this,objects)
const a=[
{
id:1,
label:"mnbmnbm1"
},
{
id:2,
label:"mnbmnbm2"
},
{
id:3,
label:"mnbmnbm3"
}
];
const b=[
{
id:1,
label:"mnbmnbm1"
},
{
id:2,
label:"mnbmnbm2"
},
{
id:4,
label:"mnbmnbm5"
}
];
const c=[
{
id:1,
label:"mnbmnbm1"
},
{
id:5,
label:"mnbmnbm5"
},
{
id:6,
label:"mnbmnbm6"
}
];
function arrayUnion(...args){
return arrayUnique(flatten(...args));
}
function flatten(...args){
return args.reduce((acc,cur)=>{
if(Array.isArray(acc) && Array.isArray(cur)){
acc=[...acc,...cur];
return acc;
}
});
}
function arrayUnique(arr){
let map={};
return arr.filter(obj =>{
let objID=obj.id;
if(!map[objID]){
map[objID]=true;
return obj;
}
});
}
console.log("result",arrayUnion(a,b,c));
Flat
和
function union(arr1, arr2) {
const union = arr1.concat.(arr2);
const uuidMap = new Map();
for (const obj of union) {
uuidMap.set(obj.uuid, obj);
}
const uuidSet = new Set(uuidMap.keys());
const result = [];
for (const uuid of uuidSet) {
const obj = uuidMap.get(uuid);
if (obj!== undefined) {
result.push(section);
}
}
}
union(arr1, arr2)