Reputation: 461
I want to compare with array 1 with array 2, array 1 has array of objects. whatever items available in array 2 those should be also available on array 1. If so, then, i have to push the item from array 2 to new array, then, i return newly created finalArray. I did the code and it is working fine as i mentioned, But, this below code executes the 'for loop' many items that i don't want.
I wanted to avoid many loop execution.
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);
Anybody help me with better way to achieve the same?
Upvotes: 1
Views: 91
Reputation: 1056
Here's a couple of options:
OPTION1:
use indexOf to avoid the second 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 = [];
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);
OPTION 2:
You could also use filter and map to do it in a more declarative way.
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);
Upvotes: 0
Reputation: 6282
You could use a more functional approach with Array.reduce
and Array.indexOf
to generate your result. This however is pretty much the same code that you have posted, but the ugly parts have been abstracted away.
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))
Upvotes: 0
Reputation: 379
You can Replace your two for loops with this
for ( var i = 0; i < a.length; i++ ) {
if (b[e].indexOf(a[i].name) != -1) {
matches.push( b[e] );
}
}
Upvotes: 0
Reputation: 68393
Replace inner for-loops with
var matches = a.filter( function(item){ return b.indexOf( item.name ) > -1 } );
For example
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))
Upvotes: 1
Reputation: 8716
If you just want to merge the arrays see Array.concat(): http://www.w3schools.com/jsref/jsref_concat_array.asp
So your code would be this: var mergedArray = arr1.concat(arr2);
This will just put them together. If you want to merge on conditions I think this isn't the best solution.
Upvotes: 0