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

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

问题描述

如果联系人中的value属性与 selectedContact 中的值匹配,我想创建一个包含联系人对象的新数组.还有更简单的方法吗?

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 ,尤其是因为已为联系人建立索引(作为数组),您正在通过索引进行选择.

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 的长度为 N ,而 selectedContacts 的长度为 M .

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

由于 selectedContacts contacts 的子集,因此我们知道 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是键,而contact是值.这将具有类似的查找速度(大致为 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天全站免登陆