如果属性与另一个数组匹配,则检索数组中的对象 [英] Retrieve objects in array if property matches another array

查看:40
本文介绍了如果属性与另一个数组匹配,则检索数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果contacts 中的value 属性与selectedContact 中的值匹配,我想创建一个包含contact 对象的新数组.有没有更简单的方法来做到这一点?

I want to create a new array containing contact objects if the value property in contacts matches the values in selectedContact. Any simpler way to do this?

selectedContact: number[] = [0,2] //value
contacts: Contact[] = [{ 
  firstName:"Dan";
  lastName:"Chong";
  email:"danc@mail.com";
  value:0;
},
{ 
  firstName:"Mark";
  lastName:"Wong";
  email:"markw@mail.com";
  value:1;
},
{ 
  firstName:"Layla";
  lastName:"Sng";
  email:"layla@mail.com";
  value: 2;
}]

预期的最终结果:

newArray = [{ 
 firstName:"Dan";
 lastName:"Chong";
 email:"danc@mail.com";
 value:0;
},{ 
 firstName:"Layla";
 lastName:"Sng";
 email:"layla@mail.com";
 value:2;
}];

我目前的解决方案:

const newArray: Contact[] = [];
this.selectedContact.forEach(index => {
  newArray.push(this.contacts.find(c => c.value === index));
});

推荐答案

在性能方面,迭代 selectedContacts 而不是 contacts 会更好,特别是因为contacts 已编入索引(作为数组),您正在通过索引进行选择.

In terms of performance, it would be better to iterate over selectedContacts rather than contacts, especially since contacts are indexed (as an array) and you are selecting through the index.

假设contacts的长度为NselectedContacts的长度为M.

Say the length of contacts is N and the length of selectedContacts is M.

由于selectedContactscontacts 的子集,我们知道M <= N.对于大型联系人数据库,这种差异可能非常显着.

Since selectedContacts is a subset of contacts, we know M <= N. For large databases of contacts, this difference could be significant.

问题中的代码:

this.selectedContact.forEach(index => {
  newArray.push(this.contacts.find(c => c.value === index));
});

O(M*N) 因为它遍历 selectedContact O(M) 并且在每次迭代中它在 <代码>联系人 (O(N)).

Has O(M*N) since it iterates over selectedContact O(M) and on each iteration it find a value in contacts (O(N)).

来自接受答案的代码遍历 contact (O(N)) 并在 selectedContact 中查找一个值,即 O(M).这使得算法等价,O(N*M)

The code from the accepted answer iterates over contact (O(N)) and looks for a value in selectedContact which is O(M). This makes the algorithm equivalent, with O(N*M)

在您的示例中,您已经有了一种按号码查找联系人的廉价方法,因为 contacts 是一个数组,而您的索引只是数组中的索引.

In your example, you already have a cheap way of looking up contacts by number since contacts is an array and your indexes are simply the index in the array.

这意味着您可以使用这样的代码:

This means you can use code like this:

return this.selectedContact.map(index => this.contacts[index]);

由于通过索引访问数组元素具有 O(1),因此这将具有 O(M),这是最小的尺寸.

Since accessing an array element by index has O(1), this would have O(M) which is the smallest of the sizes.

如果您不能使用数组索引作为键,您可以使用其他数据结构,例如 Map,其中 id 是键,联系人是值.这将具有相似的查找速度(大约 O(1)).

If you can't use the array index as a key, you can use other data structures, like a Map where the id is the key, and the contact is the value. This would have similar lookup speeds (roughly O(1)).

这篇关于如果属性与另一个数组匹配,则检索数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆