There is an array with objects "goods" for example.

someArr = [{name:***, prodid:***, prodinfo:***},{name:***, prodid:***, prodinfo:***}] 

And more information in it is different. There is another array

 someOtherArr = [{prodid:***},{prodid:***},{prodid:***}] 

there are only objects with id. How to compare these arrays and select with someArr only those objects where there is an ID with someOtherArr

    2 answers 2

    And why in the second array objects, if they contain only one field? Why not turn someOtherArr into [prodid1, prodid2, prodid3, ...] , then filtering in one line is implemented

    Es6 syntax

     const someArr = [{name:***, prodid:***, prodinfo:***}, ...] const someOtherArr = [prodid, prodid, prodid] const filtredArr = someArr.filter(el => someOtherArr.includes(el.prodid)) 

    includes just checks if there is an element in the array, if yes returns true

    If you still cannot change the structure of someOtherArr initially, then change it with your hands

     const someArr = [{name:***, prodid:***, prodinfo:***}, ...] const someOtherArr = [{prodid}, {prodid}, {prodid}] // содержит значения prodid const mapedOtherArr = someOtherArr.map(el => el.prodid) const filtredArr = someArr.filter(el => mapedOtherArr.includes(el.prodid)) 
    • thank. also useful. I used for other purposes the moment with transferring the array to this kind of const someOtherArr = [prodid, prodid, prodid] I am sure that otherwise your variant is also a worker, it was just harder for me to implement. at that time - Maxim
    • thanks again. took advantage of. Super way - Maxim

    Option a lot. Here are 1 of them.

     var someArr = [ {name:"name1", prodid:1, prodinfo:''}, {name:"name2", prodid:2, prodinfo:''}, {name:"name3", prodid:3, prodinfo:''}, {name:"name4", prodid:4, prodinfo:''}, ]; var someOtherArr = [ {prodid:1}, {prodid:3}, {prodid:2} ] var result = []; for(var i = 0; i < someOtherArr.length; i++) { result.push(someArr.find(function (item) { return item.prodid == someOtherArr[i].prodid })) } console.log(result); 

    • thanks, very helpful. - Maxim