0

I need to get filtrated array that have products only with ids from first array. I tried something like this:

let first = [1, 4]
let second = [{id: 1}, {id: 2}, {id: 4}]
second.filter((es, i) => es.id.includes(first))

enter image description here

shiroSn
  • 29
  • 7
  • 1
    You should be doing (es, i) => first.includes(es.id). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes – Kavian Rabbani Feb 09 '23 at 07:22

1 Answers1

2

you will need to reverse the include condition so it should be first.includes(es.id) instead of es.id.includes(first)

let first = [1, 4]
let second = [{id: 1}, {id: 2}, {id: 4}]
let result=second.filter(es => first.includes(es.id))
console.log(result);
jitender
  • 10,238
  • 1
  • 18
  • 44