[英]Best way to find matching id's in an array of objects?
if I have this array of movie ids如果我有这个电影 ID 数组
movies = [28, 14, 100, 53, 37]
and this array of objects.和这个对象数组。
genres = [
{id: 28, name: "Action"},
{id: 10770, name: "TV Movie"},
{id: 53, name: "Thriller"},
{id: 10752, name: "War"},
{id: 37, name: "Western"}
]
I would like to return an array of the matching ids.我想返回一个匹配 id 的数组。 example [ 'Action', 'Thriller', 'Western' ].例如['动作','惊悚','西部']。
I have a solution already but feel that it could be better.我已经有一个解决方案,但觉得它可能会更好。 What is the best way to refactor this code?重构此代码的最佳方法是什么? Thanks.谢谢。
genre_array = []
movies.forEach(function(e){
genres.forEach(function(element){
if (element.id == e) {
genre_array.push(element.name)
}
});
});
I would combine the filter
and map
array methods.我会结合filter
和map
数组方法。 Use filter
to get a list of genres that are in your movies
array, then use map
to convert that to a list of names.使用filter
获取movies
数组中的流派列表,然后使用map
将其转换为名称列表。
Example:例子:
const movies = [28, 14, 100, 53, 37] const genres = [ {id: 28, name: "Action"}, {id: 10770, name: "TV Movie"}, {id: 53, name: "Thriller"}, {id: 10752, name: "War"}, {id: 37, name: "Western"} ] // I would like to return an array of the matching ids. example [ 'Action', 'Thriller', 'Western' ]. console.log(genres.filter(g => movies.includes(g.id)).map(g => g.name))
Convert array=movies
to Set
first (it will improve performances when array=movies
has a ton of elements), then use reduce
to pull out all match items.首先将array=movies
转换为Set
(当array=movies
有大量元素时会提高性能),然后使用reduce
拉出所有匹配项。
let movies = [28, 14, 100, 53, 37, 28] let genres = [ {id: 28, name: "Action"}, {id: 10770, name: "TV Movie"}, {id: 53, name: "Thriller"}, {id: 10752, name: "War"}, {id: 37, name: "Western"} ] let indexes = new Set(movies) console.log( genres.reduce((pre, cur) => { indexes.has(cur.id) && pre.push(cur.name) return pre }, []) )
Use an array reducer to match ids together使用数组 reducer将 id 匹配在一起
const movies = [28, 14, 100, 53, 37] const genres = [ {id: 28, name: "Action"}, {id: 10770, name: "TV Movie"}, {id: 53, name: "Thriller"}, {id: 10752, name: "War"}, {id: 37, name: "Western"} ] let genre_array = genres.reduce((arr, itm) => movies.includes(itm.id) ? arr.concat(itm.name) : arr, []) console.log(genre_array)
Simple:简单的:
const movies = [28, 14, 100, 53, 37] const genres = [{ id: 28, name: "Action" }, { id: 10770, name: "TV Movie" }, { id: 53, name: "Thriller" }, { id: 10752, name: "War" }, { id: 37, name: "Western" } ] let genre_array = []; genres.forEach(function(element) { if (movies.includes(element.id)) { genre_array.push(element.name) } }); alert(genre_array);
Filter and map shorthand过滤器和映射速记
const movies = [28, 14, 100, 53, 37], genres = [ {id: 28, name: "Action"}, {id: 10770, name: "TV Movie"}, {id: 53, name: "Thriller"}, {id: 10752, name: "War"}, {id: 37, name: "Western"} ], genreList = genres // filter and a map - shorthand .filter(({id}) => movies.includes(id)) .map(({name}) => name); console.log(genreList);
I bumped into a very similar problem this morning and took time out to address one possible oversight, in that the order of the original array is lost with the given solutions.今天早上我遇到了一个非常相似的问题,并抽出时间来解决一个可能的疏忽,因为给定的解决方案会丢失原始数组的顺序。
So I did some digging around and discussion with others (thanks discord,);所以我做了一些挖掘并与其他人讨论(感谢 discord,); I've come up with this solution that;我想出了这个解决方案;
const movies = [37, 28, "bad data", false ,, 53, 53];
const genres = [
{
"id": 28,
"name": "Action"
}, {
"id": 10770,
"name": "TV Movie"
},{
"id": 53,
"name": "Thriller"
},{
"id": 10752,
"name": "War"
}, {
"id": 37,
"name": "Western"
}];
const genreList = new Set(
movies
.map( element => genres
.find( objectElement => objectElement.id === element)?.name)
.filter(Boolean));
//Return is [ 'Western', 'Action', 'Thriller' ]
console.log(Array.from(genreList));
Importantly, note how the order of the returned array matches the order of the original IDs.重要的是,请注意返回数组的顺序如何与原始 ID 的顺序相匹配。
Appreciated the other solutions posted.感谢发布的其他解决方案。 There are so many ways to achieve so much greatness.有很多方法可以实现如此伟大。 I would love to hear of where my solution could possibly be further improved.我很想知道我的解决方案在哪里可以进一步改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.