[英]Map though an objects array to filter an Array in JavaScript
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.我有一个 id,我想通过一个数组 map 并获得一个新数组,其中只有包含 id 项将在那里。
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:现在,假设我的 id 是 28。我只想要包含 id 28 的项目。所以,我的 output 应该是这样的:
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 }) })
You can do it by using filter
and includes
method on array, like this:您可以通过在数组上使用filter
和includes
方法来做到这一点,如下所示:
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)
I suppose that you have a variable called ID
我想你有一个名为ID
的变量
Using filter
to return new array after filtering过滤后使用filter
返回新数组
The condition is genre_ids
include ID
条件是genre_ids
包含ID
const ID = 28
const filteredList = list.filter(item => item.genre_ids.includes(ID))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.