Home > Blockchain >  Map though an objects array to filter an Array in JavaScript
Map though an objects array to filter an Array in JavaScript

Time:12-26

I have an id and with that i want to map through an array and get a new array where only the containing id items will be there.

So,the array is like :

var list = [
     {
      genre_ids: [28, 12, 878],
      genres: undefined,
      id: 634649,
      image:        "http://image.tmdb.org/t/p/w342//1g0dhYtq4irTY1GPXvft6k4YLjm.jpg",
      popularity: 12971.91,
      released: "2021-12-15",
      title: "Spider-Man: No Way Home",
      vote_average: 8.5,
      vote_count: 2295
    },
    { 
      genre_ids: [878, 28, 12],
      genres: undefined,
      id: 580489,
      image: "http://image.tmdb.org/t/p/w342//rjkmN1dniUHVYAtwuV3Tji7FsDO.jpg",
      popularity: 6303.047,
      released: "2021-09-30"
      title: "Venom: Let There Be Carnage"
      vote_average: 7.2
      vote_count: 5012
     },
     {
      genre_ids: [16, 35, 10751, 14],
      genres: undefined,
      id: 568124,
      image: "http://image.tmdb.org/t/p/w342//4j0PNHkMr5ax3IA8tjtxcmPU3QT.jpg",
      popularity: 3157.171,
      released: "2021-11-24",
      title: "Encanto",
      vote_average: 7.6,
      vote_count: 541
     }
    ]

Now, suppose my id is 28. I want only the items containing id 28. So, my output should be like this:

    var list = [
         {
          genre_ids: [28, 12, 878],
          genres: undefined,
          id: 634649,
          image:        "http://image.tmdb.org/t/p/w342//1g0dhYtq4irTY1GPXvft6k4YLjm.jpg",
          popularity: 12971.91,
          released: "2021-12-15",
          title: "Spider-Man: No Way Home",
          vote_average: 8.5,
          vote_count: 2295
        },
        { 
          genre_ids: [878, 28, 12],
          genres: undefined,
          id: 580489,
          image: "http://image.tmdb.org/t/p/w342//rjkmN1dniUHVYAtwuV3Tji7FsDO.jpg",
          popularity: 6303.047,
          released: "2021-09-30"
          title: "Venom: Let There Be Carnage"
          vote_average: 7.2
          vote_count: 5012
         },
         
        ]

Here's what i have tried:

 var newGenre = list.map((item)=>{
      return item.genre_ids.map((gId)=>{
        gId === 28
      })
 })

CodePudding user response:

I suppose that you have a variable called ID

Using filter to return new array after filtering

The condition is genre_ids include ID

const ID = 28
const filteredList = list.filter(item => item.genre_ids.includes(ID))

CodePudding user response:

You can do it by using filter and includes method on array, like this:

let list = [{ genre_ids: [28, 12, 878], genres: undefined, id: 634649, image: "http://image.tmdb.org/t/p/w342//1g0dhYtq4irTY1GPXvft6k4YLjm.jpg", popularity: 12971.91, released: "2021-12-15", title: "Spider-Man: No Way Home", vote_average: 8.5, vote_count: 2295 }, { genre_ids: [878, 28, 12], genres: undefined, id: 580489, image: "http://image.tmdb.org/t/p/w342//rjkmN1dniUHVYAtwuV3Tji7FsDO.jpg", popularity: 6303.047, released: "2021-09-30", title: "Venom: Let There Be Carnage", vote_average: 7.2, vote_count: 5012, }, { genre_ids: [16, 35, 10751, 14], genres: undefined, id: 568124, image: "http://image.tmdb.org/t/p/w342//4j0PNHkMr5ax3IA8tjtxcmPU3QT.jpg", popularity: 3157.171, released: "2021-11-24", title: "Encanto", vote_average: 7.6, vote_count: 541 } ];
const targetId = 28;
let result = list.filter(item => item.genre_ids.includes(targetId));
console.log(result)

  • Related