admin管理员组文章数量:1426202
I have an array such as :
const cars = [
{ id: 23423, brand: 'bmw', doors: 2, color: 'red' },
{ id: 23452, brand: 'volvo', doors: 4, color: 'gray' },
{ id: 97456, brand: 'citroen', doors: 4, color: 'black' },
{ id: 45784, brand: 'dodge', doors: 2, color: 'red' },
{ id: 23452, brand: 'ferrari', doors: 2, color: 'red' },
{ id: 23522, brand: 'bmw', doors: 2, color: 'blue' }
];
Here i have a list of ids such as : [45784, 23522]
I'd like to find the best way using map, filter or reduce to retrieve the items in the first array using the ids array.
Thanks
I have an array such as :
const cars = [
{ id: 23423, brand: 'bmw', doors: 2, color: 'red' },
{ id: 23452, brand: 'volvo', doors: 4, color: 'gray' },
{ id: 97456, brand: 'citroen', doors: 4, color: 'black' },
{ id: 45784, brand: 'dodge', doors: 2, color: 'red' },
{ id: 23452, brand: 'ferrari', doors: 2, color: 'red' },
{ id: 23522, brand: 'bmw', doors: 2, color: 'blue' }
];
Here i have a list of ids such as : [45784, 23522]
I'd like to find the best way using map, filter or reduce to retrieve the items in the first array using the ids array.
Thanks
Share Improve this question asked Mar 28, 2018 at 15:04 Louis LecocqLouis Lecocq 1,8143 gold badges18 silver badges40 bronze badges 1- 1 how do you define "best way" – zfrisch Commented Mar 28, 2018 at 15:05
3 Answers
Reset to default 8You could filter by using Array#includes
.
This proposal uses a destructuring assignment for the id
property of the object.
var cars = [{ id: 23423, brand: 'bmw', doors: 2, color: 'red' }, { id: 23452, brand: 'volvo', doors: 4, color: 'gray' }, { id: 97456, brand: 'citroen', doors: 4, color: 'black' }, { id: 45784, brand: 'dodge', doors: 2, color: 'red' }, { id: 23452, brand: 'ferrari', doors: 2, color: 'red' }, { id: 23522, brand: 'bmw', doors: 2, color: 'blue' }],
ids = [45784, 23522],
result = cars.filter(({ id }) => ids.includes(id));
console.log(result);
Simple as:
const
cars = [
{ id: 23423, brand: 'bmw', doors: 2, color: 'red' },
{ id: 23452, brand: 'volvo', doors: 4, color: 'gray' },
{ id: 97456, brand: 'citroen', doors: 4, color: 'black' },
{ id: 45784, brand: 'dodge', doors: 2, color: 'red' },
{ id: 23452, brand: 'ferrari', doors: 2, color: 'red' },
{ id: 23522, brand: 'bmw', doors: 2, color: 'blue' }
],
ids = [45784, 23522];
let arr = cars.filter(elem => ids.includes(elem.id));
console.log(arr);
Convert a Set from the ids
array, and use Array.filter()
to take only items that exist in the Set:
const cars = [
{ id: 23423, brand: 'bmw', doors: 2, color: 'red' },
{ id: 23452, brand: 'volvo', doors: 4, color: 'gray' },
{ id: 97456, brand: 'citroen', doors: 4, color: 'black' },
{ id: 45784, brand: 'dodge', doors: 2, color: 'red' },
{ id: 23452, brand: 'ferrari', doors: 2, color: 'red' },
{ id: 23522, brand: 'bmw', doors: 2, color: 'blue' }
];
const ids = [45784, 23522];
const idsSet = new Set(ids);
const result = cars.filter(({ id }) => idsSet.has(id));
console.log(result);
本文标签: javascriptRetrieve several items from a list by idsStack Overflow
版权声明:本文标题:javascript - Retrieve several items from a list by ids - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745426602a2658141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论