filter object array based on string array

Asked

Viewed 71 times

0

Hello I have an array of objects like this:

product: [
  { id: "1", name: "product 01" },
  { id: "2", name: "product 02" },
  { id: "3", name: "product 03" },
  { id: "4", name: "product 04" },
  { id: "5", name: "product 05" }

]

list: [ "1", "2", "5" ]

I would like a result similar to this, where it filters only the objects that are registered in the list by Id:

newArray: [
  { id: "1", name: "product 01" },
  { id: "2", name: "product 02" },
  { id: "5", name: "product 05" }

]

I think you have to use the filter with the map, but I’m having trouble making that union

  • did not understand the final result by list: ["1", "2"] and the result is 1, 2 and 5?

  • It was recently edited: "[4] Improve syntax Highlighting" and included these extra values. However, I believe that for the result with ID: 5, it should be like this the list: ['1', '2', '5'].

  • Avoid putting text as a photo because it makes it difficult to read on some devices. Copy and paste the output.

  • really, I’m sorry. The mistake was mine... I’ll fix

  • Now it made sense!

2 answers

3


Use the filter()

The indexOf() returns -1 if the element you are looking for does not exist in the array. Then you can use it together with the filter() to fetch an array of Ids in an array of objects.

product.filter(item => list.indexOf(item.id) !== -1)
// Output:
// [
//   { "id": "1", "name": "product 01" },
//   { "id": "2", "name": "product 02" }
// ]

View screenshot

  • Why would I have to put the !== -1, because when I went to test in my code the result only brought the last product, and when I put the !== -1, brought the correct result. I thought he was just in case he didn’t find the result and wouldn’t interfere so much. Because this occurs?

  • Hi, @Sarahrodrigues. In this case, the indexOf() returns the position in the array of the element you are looking for. That is, if you are searching for the Ids ['1', '2'] in an object array [{'id': '1'}, {'id': '2'}, {'id': '3'}], the indexOf() will return respectively 0 and 1 for the Ids found, and -1 for the ID: 3 you are not searching for. O !== serves to compare the value and type at the same time, a security check. But in case if you do not put !== -1, all that is not true 0. I believe it would return Ids 2 and 3, which are wrong.

  • 1

    Ahhh got it!!! Thank you very much!!!

1

Maybe you want this:

Parameters for research:
Array Filter
Array Includes

const products = [
   {id:1, name: 'product 1'},
   {id:2, name: 'product 2'},
   {id:3, name: 'product 3'}
];
const newProduct = products.filter((product) => product.id < 3)

console.log(products)   
console.log(newProduct)

idFilter = [1, 2]
const filterProduct = products.filter((product) => idFilter.includes(product.id))    

console.log(filterProduct)

inserir a descrição da imagem aqui

  • Unfortunately not, both the list of products and the list can have several values, I will change a little to you understand.

  • I updated the answer, maybe I’ll answer you now, I left the previous example, so I can compare the use only of the filter and the use of the filter with incluedes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.