问题: 我想获取对象数组的 inetrsection。
var a = [{id: 1, name: 'jake'}];
var b = [{id: 1, name: 'jake'}, {id: 4,name: 'jenny'}];
var c = [{id: 1,name: 'jake'}, {id: 4,name: 'jenny'}, {id: 9,name: 'nick'}];
intersect (a,b,c);// Find Intersection based on id key
// answer would be [{id: 1, name: 'jake'}]
我在这里找到了这个非常有帮助的答案 How to use underscore's "intersection" on objects?
但是 这个解决方案使用 underscore.js,而我使用 jquery。
我似乎不知道 _.any 在做什么。 任何帮助将不胜感激。
这是完整的代码
代码: http://jsfiddle.net/luisperezphd/43vksdn6/
function intersectionObjects2(a, b, areEqualFunction) {
var results = [];
for(var i = 0; i < a.length; i++) {
var aElement = a[i];
var existsInB = _.any(b, function(bElement) { return areEqualFunction(bElement, aElement); });
if(existsInB) {
results.push(aElement);
}
}
return results;
}
function intersectionObjects() {
var results = arguments[0];
var lastArgument = arguments[arguments.length - 1];
var arrayCount = arguments.length;
var areEqualFunction = _.isEqual;
if(typeof lastArgument === "function") {
areEqualFunction = lastArgument;
arrayCount--;
}
for(var i = 1; i < arrayCount ; i++) {
var array = arguments[i];
results = intersectionObjects2(results, array, areEqualFunction);
if(results.length === 0) break;
}
return results;
}
var a = [ { id: 1, name: 'jake' }, { id: 4, name: 'jenny'} ];
var b = [ { id: 1, name: 'jake' }, { id: 9, name: 'nick'} ];
var c = [ { id: 1, name: 'jake' }, { id: 4, name: 'jenny'}, { id: 9, name: 'nick'} ];
var result = intersectionObjects(a, b, c, function(item1, item2) {
return item1.id === item2.id;
});
最佳答案
此解决方案对具有相同属性的相同给定对象进行计数,如果它们在两个数组 intersection()
中,则返回它们。
function intersection(a, b, key) {
function count(a) {
o[a[key]] = o[a[key]] || { value: a, count: 0 };
o[a[key]].count++;
}
var o = {}, r = [];
a.forEach(count);
b.forEach(count);
Object.keys(o).forEach(function (k) {
o[k].count === 2 && r.push(o[k].value);
});
return r;
}
function intersect(a, b, c, key) {
return intersection(intersection(a, b, key), c, key);
}
var a = [{ id: 1, name: 'jake' }],
b = [{ id: 1, name: 'jake' }, { id: 4, name: 'jenny' }],
c = [{ id: 1, name: 'jake' }, { id: 4, name: 'jenny' }, { id: 9, name: 'nick' }],
result = intersect(a, b, c, 'id');
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
现在可以使用这种风格的回调。
function (v) {
return v.id;
}
它需要返回一个可字符串值,并且可以包含其他值和组合,例如与姓名和年龄相交的示例(如果数据中存在):
function (v) {
return v.name + '|' + v.age;
}
function intersection(a, b, cb) {
function count(a) {
o[cb(a)] = o[cb(a)] || { value: a, count: 0 };
o[cb(a)].count++;
}
var o = {}, r = [];
a.forEach(count);
b.forEach(count);
Object.keys(o).forEach(function (k) {
o[k].count === 2 && r.push(o[k].value);
});
return r;
}
function intersect(a, b, c, key) {
return intersection(intersection(a, b, key), c, key);
}
var a = [{ id: 1, name: 'jake' }],
b = [{ id: 1, name: 'jake' }, { id: 4, name: 'jenny' }],
c = [{ id: 1, name: 'jake' }, { id: 4, name: 'jenny' }, { id: 9, name: 'nick' }],
result = intersect(a, b, c, function (_) { return _.id; });
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
关于javascript - 基于下划线js的函数转换为PlainJs或jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35481788/