我想将数组1与数组2进行比较,数组1包含对象数组。数组2中可用的任何项目也应该在数组1上可用。如果是这样,那么,我必须将项目从数组2推送到新数组,然后,我返回新创建的finalArray。 我做了代码,它正如我提到的那样工作正常,但是,下面的代码执行'for loop'许多我不想要的项目。
我想避免许多循环执行。
var arr1 = [{"count":1,"name":"hitler"},{"count":1,"name":"cool"},{"count":1,"name":"cooola"},{"count":1,"name":"cute"},{"count":1,"name":"nyle"},{"count":1,"name":""},{"count":1,"name":"path"},{"count":1,"name":"root"},{"count":1,"name":"sssstag"},{"count":1,"name":"ssssu tag"},{"count":1,"name":"sutag"},{"count":1,"name":"tag2"},{"count":1,"name":"arrogant"},{"count":1,"name":"test01"},{"count":1,"name":"test10"},{"count":1,"name":"uber"},{"count":1,"name":"union"},{"count":1,"name":"assettag"},{"count":1,"name":"wire"}];
function intersect_arrays(a, b) {
var matches = [];
for ( var i = 0; i < a.length; i++ ) {
for ( var e = 0; e < b.length; e++ ) {
if ( a[i].name === b[e] ) matches.push( b[e] );
}
}
return matches;
}
var arr2 = ["hilter","arrogant","cool","uber"];
var finalArray = intersect_arrays(arr1, arr2);
console.log(finalArray);
有人帮助我更好地实现同样的目标吗?
答案 0 :(得分:1)
用
替换内部for循环var matches = a.filter( function(item){ return b.indexOf( item.name ) > -1 } );
例如
var arr1 = [{
"count": 1,
"name": "hitler"
}, {
"count": 1,
"name": "cool"
}, {
"count": 1,
"name": "cooola"
}, {
"count": 1,
"name": "cute"
}, {
"count": 1,
"name": "nyle"
}, {
"count": 1,
"name": ""
}, {
"count": 1,
"name": "path"
}, {
"count": 1,
"name": "root"
}, {
"count": 1,
"name": "sssstag"
}, {
"count": 1,
"name": "ssssu tag"
}, {
"count": 1,
"name": "sutag"
}, {
"count": 1,
"name": "tag2"
}, {
"count": 1,
"name": "arrogant"
}, {
"count": 1,
"name": "test01"
}, {
"count": 1,
"name": "test10"
}, {
"count": 1,
"name": "uber"
}, {
"count": 1,
"name": "union"
}, {
"count": 1,
"name": "assettag"
}, {
"count": 1,
"name": "wire"
}];
var arr2 = ["hilter", "arrogant", "cool", "uber"];
function intersect_arrays(a, b) {
var matches = a.filter(function(item) {
return b.indexOf(item.name) > -1
});
matches = matches.map( function(item){ return item.name } );
return matches;
}
console.log(intersect_arrays(arr1, arr2))
&#13;
答案 1 :(得分:0)
如果您只想合并数组,请参阅Array.concat(): http://www.w3schools.com/jsref/jsref_concat_array.asp
所以你的代码是这样的: var mergedArray = arr1.concat(arr2);
这将把它们组合在一起。如果你想在条件上合并,我认为这不是最好的解决方案。
答案 2 :(得分:0)
您可以使用此
替换两个for循环for ( var i = 0; i < a.length; i++ ) {
if (b[e].indexOf(a[i].name) != -1) {
matches.push( b[e] );
}
}
答案 3 :(得分:0)
您可以使用Array.reduce
和Array.indexOf
更具功能性的方法来生成结果。然而,这与您发布的代码几乎相同,但丑陋的部分已经被抽象掉了。
const arr1 = [
{"count":1,"name":"hitler"},{"count":1,"name":"cool"},
{"count":1,"name":"cooola"},{"count":1,"name":"cute"},
{"count":1,"name":"nyle"},{"count":1,"name":""},
{"count":1,"name":"path"},{"count":1,"name":"root"},
{"count":1,"name":"sssstag"},{"count":1,"name":"ssssu tag"},
{"count":1,"name":"sutag"},{"count":1,"name":"tag2"},
{"count":1,"name":"arrogant"},{"count":1,"name":"test01"},
{"count":1,"name":"test10"},{"count":1,"name":"uber"},
{"count":1,"name":"union"},{"count":1,"name":"assettag"},
{"count":1,"name":"wire"}
];
const arr2 = ["hilter","arrogant","cool","uber"];
const intersect_arrays = function(arr, keys) {
// reduce the array of objects
return arr.reduce((acc, x) => {
// check if the name property is contained in the keys array
if (~keys.indexOf(x.name)) {
// add the name to the returned values
return acc.concat(x.name)
}
return acc
}, [])
}
console.log(intersect_arrays(arr1, arr2))
答案 4 :(得分:0)
以下是几个选项:
OPTION1 :
使用 indexOf 来避免第二次循环。
var arr1 = [{"count":1,"name":"hitler"},{"count":1,"name":"cool"},{"count":1,"name":"cooola"},{"count":1,"name":"cute"},{"count":1,"name":"nyle"},{"count":1,"name":""},{"count":1,"name":"path"},{"count":1,"name":"root"},{"count":1,"name":"sssstag"},{"count":1,"name":"ssssu tag"},{"count":1,"name":"sutag"},{"count":1,"name":"tag2"},{"count":1,"name":"arrogant"},{"count":1,"name":"test01"},{"count":1,"name":"test10"},{"count":1,"name":"uber"},{"count":1,"name":"union"},{"count":1,"name":"assettag"},{"count":1,"name":"wire"}];
function intersect_arrays(a, b) {
var matches = [];
a.forEach(function(item){
if(b.indexOf(item.name) > -1) {
matches.push(item.name);
}
});
return matches;
}
var arr2 = ["hilter", "arrogant", "cool", "uber"];
var finalArray = intersect_arrays(arr1, arr2);
console.log(finalArray);
选项2:
您还可以使用过滤器和地图以更具说明性的方式执行此操作。
var arr1 = [{"count":1,"name":"hitler"},{"count":1,"name":"cool"},{"count":1,"name":"cooola"},{"count":1,"name":"cute"},{"count":1,"name":"nyle"},{"count":1,"name":""},{"count":1,"name":"path"},{"count":1,"name":"root"},{"count":1,"name":"sssstag"},{"count":1,"name":"ssssu tag"},{"count":1,"name":"sutag"},{"count":1,"name":"tag2"},{"count":1,"name":"arrogant"},{"count":1,"name":"test01"},{"count":1,"name":"test10"},{"count":1,"name":"uber"},{"count":1,"name":"union"},{"count":1,"name":"assettag"},{"count":1,"name":"wire"}];
function intersectArrays(a, b) {
return a.filter(function(item){
return (b.indexOf(item.name) > -1)
});
}
var arr2 = ["hilter", "arrogant", "cool", "uber"];
var finalArray = intersectArrays(arr1, arr2).map(function(item) {
return item.name;
});
console.log(finalArray);