I have two arrays of objects, and I want to filter the first one according to whats on the second one. Here's an example:
var ary1 = [{id: 23, title: 'blabla'},{id:43, title: 'bleble'}, {id:54, title:'blibli'}];
var ary2 = [{id:23},{id:54}, {id:65}];
So in this case what I want to return is an array with the objects that have id's 23 and 54 of the first array, with all its possible properties (in this case, title).
Could you give me any hint that could help me?
-
Please share the code of what you have tried.Josh Harrison– Josh Harrison11/20/2014 11:47:02Commented Nov 20, 2014 at 11:47
4 Answers 4
Get a list of the indexes you want to search on using map
:
var indexes = ary2.map(function (el) {
return el.id;
});
filter
the results based on the list of indexes:
var result = ary1.filter(function (el) {
return indexes.indexOf(el.id) > -1;
});
-
1Why is this getting upvotes and my answer not? I did the exact same thing in my answer 8 minutes before this answer?Arthur Weborg– Arthur Weborg11/20/2014 12:10:39Commented Nov 20, 2014 at 12:10
-
1It might be because you combined the
map
in thefilter
rather than have them as two distinct operations which makes it easier (in my mind) to understand.Andy– Andy11/20/2014 12:19:53Commented Nov 20, 2014 at 12:19 -
2Perhaps it's because your solution redoes the map each iteration.user663031– user66303111/20/2014 13:15:46Commented Nov 20, 2014 at 13:15
-
1@Allonsy Do note that if you still need to support IE8 (or God forbid, anything worse) you will need to use these polyfills for Array.prototype.map() and Array.prototype.filter()Josh Harrison– Josh Harrison11/20/2014 15:34:59Commented Nov 20, 2014 at 15:34
This might help you.
Loop through
ary2
, building up an array of eachid
value (let's call this arrayexistingIds
).After that loop, now loop through
ary1
. For each item inary1
, check to see if theid
value exists in theexistingIds
array that we just built up. If it does, append the current item to aresult
array.
I could write the code for you, but it will be a better learning experience if you first try this yourself :)
Might as well make use of some functional programming built into javascript.
filteredResults = ary1.filter(function(ele){
return (ary2.map(function(idobj){return idobj.id;}).indexOf(ele.id)>-1)
})
filter(function)
will iterate through each element of an array, passing it through a callback function. From within that callback iff a true is returned, that value is kept. If false, that value is filtered out.
Also map(function)
will iterate through each element of an array passing a callback value as well. All values returned from map callback will be injected into the result. So we can take the id from each element in ary2 and return it in the map
function.
var ary1 = [{id: 23, title: 'blabla'},{id:43, title: 'bleble'}, {id:54, title:'blibli'}];
var ary2 = [{id:23},{id:54}, {id:65}];
//Filter for the available ID's, store the resulting objects in a new array
filteredResults = ary1.filter(function(ele){
//map creates an array of just ID's
return (ary2.map(function(idobj){return idobj.id;}).indexOf(ele.id)>-1)
})
//now do whatever you were planning on doing with your results/
var res = document.getElementById("results");
filteredResults.forEach(function(ele){
res.innerHTML+="<li>{id:"+ele.id + ",title:" +ele.title+"}</li>"
})
console.log(filteredResults);
<ul id="results"></ul>
try this:
var ary1 = [{id: 23, title: 'blabla'},{id:43, title: 'bleble'}, {id:54, title:'blibli'}];
var ary2 = [{id:23},{id:54}, {id:65}];
var newary=[];
for(x in ary1){
for(y in ary2){
if(ary1[x].id == ary2[y].id){
newary.push(ary1[x]);
}
}
}
console.log(newary);// here newary will be your return newary;