简体   繁体   English

如果属性与另一个数组匹配,则检索数组中的对象

[英]Retrieve objects in array if property matches another array

I want to create a new array containing contact objects if the value property in contacts matches the values in selectedContact . 如果联系人中的value属性与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;
}]

Intended final result: 预期的最终结果:

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

My current solution: 我当前的解决方案:

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

You can use Array.prototype.filter() 您可以使用Array.prototype.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function. filter()方法创建一个新数组,其中所有元素都通过了由提供的函数实现的测试。

and Array.prototype.includes() Array.prototype.includes()

The includes() method determines whether an array includes a certain element, returning true or false as appropriate. includes()方法确定数组是否包含某个元素,并在适当时返回true或false。

Working Code Example: 工作代码示例:

 var selectedContact = [0,2]; var contacts = [{ 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 }] let newArray = contacts.filter(c => selectedContact.includes(c.value)); console.log(newArray); 

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. 就性能而言,最好在selectedContacts上进行迭代,而不要在contacts上进行迭代,尤其是因为已对contacts索引(作为数组)并且您正在通过索引进行选择。

Say the length of contacts is N and the length of selectedContacts is M . 假设contacts的长度为N ,而selectedContacts的长度为M

Since selectedContacts is a subset of contacts , we know M <= N . 由于selectedContactscontacts的子集,因此我们知道M <= N For large databases of contacts, this difference could be significant. 对于大型的联系人数据库,这种差异可能会很大。

The code in the question: 问题中的代码:

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

Has O(M*N) since it iterates over selectedContact O(M) and on each iteration it find a value in contacts ( O(N) ). 具有O(M*N)因为它遍历selectedContact O(M)并且在每次迭代时都在contactsO(N) )中找到一个值。

The code from the accepted answer iterates over contact ( O(N) ) and looks for a value in selectedContact which is O(M) . 接受的答案中的代码遍历contactO(N) ),并在selectedContact寻找值为O(M) This makes the algorithm equivalent, with O(N*M) 这使得算法等效于O(N*M)

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. 在您的示例中,由于contacts是一个数组,而您的索引只是该数组中的索引,因此您已经有一种便宜的方法来按号码查找联系人。

This means you can use code like this: 这意味着您可以使用如下代码:

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

Since accessing an array element by index has O(1) , this would have O(M) which is the smallest of the sizes. 由于按索引访问数组元素具有O(1) ,因此它将具有O(M) ,这是最小的大小。

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. 如果不能将数组索引用作键,则可以使用其他数据结构,例如Map ,其中id是键,而contact是值。 This would have similar lookup speeds (roughly O(1) ). 这将具有相似的查找速度(大约为O(1) )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回与另一个对象数组的属性匹配的对象数组的子集 - Return a subset of an array of objects that matches a property of an another array of objects 在数组中设置对象属性 true/false,无论 id 是否与另一个对象数组中的任何 id 匹配 - Set object property in an array true/false, whether the id matches with any id from another array of objects 如何检查数组对象的属性是否与另一个 object 数组中的值之一匹配 - How to check if property of an objects of array matches with one of the values in another array of object 如何在具有对象的数组中查找匹配项并更改属性? - How to find matches in an array with objects and change property? 从数组中检索对象,其中另一个对象与另一个数组匹配 - retrieve object from array WHERE another object matches another array TS/JS - 如果数组中对象的属性与单独对象中的另一个属性匹配,则从对象数组中获取“值” - TS/JS - Get "value" from an Array of Objects if a Property of an Object from the Array matches another Property from a separate Object 如何返回属性与数组匹配的对象数组? - How can I return an array of objects whose property matches an array? Javascript从属性值匹配的对象数组返回数组 - Javascript return array from array of objects where property value matches 如果变量的值与数组对象的属性匹配,则填充和数组 - Populated and array if value of variable matches with property of an array Objects 向另一个数组的对象数组添加属性 - Adding a property to an array of objects from another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM