【问题标题】:Map though an objects array to filter an Array in JavaScript通过对象数组映射以过滤 JavaScript 中的数组
【发布时间】:2022-01-25 14:23:49
【问题描述】:

我有一个 id,我想通过它映射一个数组并获得一个新数组,其中只有包含 id 的项目将在那里。

所以,数组是这样的:

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
     }
    ]

现在,假设我的 id 是 28。我只想要包含 id 28 的项目。所以,我的输出应该是这样的:

    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
         },
         
        ]

这是我尝试过的:

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

【问题讨论】:

  • 试试filter + includes: list.filter(i => i.genre_ids.includes(28))

标签: javascript arrays


【解决方案1】:

您可以通过在数组上使用filterincludes 方法来做到这一点,如下所示:

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)

【讨论】:

    【解决方案2】:

    我想你有一个名为ID的变量

    使用filter过滤后返回新数组

    条件为genre_ids包含ID

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2020-10-24
      • 2020-12-03
      • 2019-11-21
      • 2022-07-25
      • 1970-01-01
      相关资源
      最近更新 更多