[英]Set object property in an array true/false, whether the id matches with any id from another array of objects
So first, here's a simple snippet to demonstrate what I mean exactly, and what I have tried.首先,这里有一个简单的片段来演示我的确切意思以及我尝试过的内容。
let array_1 = [ { id: 1, name: 'Peter' }, { id: 2, name: 'John' }, { id: 3, name: 'Andrew' }, { id: 4, name: 'Patrick' }, { id: 5, name: 'Brian' } ]; let array_2 = [ { id: 1, name: 'not Peter' }, { id: 80, name: 'not John' }, { id: 3, name: 'not Andrew' }, { id: 40, name: 'not Patrick' }, { id: 5, name: 'not Brian' } ]; array_1.forEach(item_1 => { for (let i = 0; i < array_2.length; i++) { item_1.matches = array_2[i].id === item_1.id } }); console.log('matched_array', array_1);
The goal here is to add the matches
property to each object in array_1
and set it to true
/ false
, based on whether the id
matches with any other id
from array_2
.这里的目标是根据
id
是否与array_2
任何其他id
匹配,将matches
属性添加到array_1
每个对象并将其设置为true
/ false
。
In this current example, the result of the matches
properties should go like this: true - false - true - false - true
.在当前示例中,
matches
属性的结果应如下所示: true - false - true - false - true
。 But my current code only sets this property correctly in the last element of the array ( array_1
).但是我当前的代码只在数组的最后一个元素(
array_1
)中正确设置了这个属性。 Obviously it's because my code is not entirely correct, and that's where I'm stuck.显然这是因为我的代码不完全正确,这就是我被卡住的地方。
You could first create one object with reduce
method that you can then use as a hash table to check if the element with the same id exists in the array 2.您可以首先使用
reduce
方法创建一个对象,然后您可以将其用作哈希表来检查数组 2 中是否存在具有相同 id 的元素。
let array_1=[{"id":1,"name":"Peter"},{"id":2,"name":"John"},{"id":3,"name":"Andrew"},{"id":4,"name":"Patrick"},{"id":5,"name":"Brian"}, {"id":6,"name":"Joe"}] let array_2=[{"id":1,"name":"not Peter"},{"id":80,"name":"not John"},{"id":3,"name":"not Andrew"},{"id":40,"name":"not Patrick"},{"id":5,"name":"not Brian"}] const o = array_2.reduce((r, e) => (r[e.id] = true, r), {}) const result = array_1.map(e => ({ ...e, matches: o[e.id] || false})) console.log(result)
I would first collect the ids of array_2
in a Set , sets have a O(1) lookup time so checking if an id is in this set is fast.我会首先在Set 中收集
array_2
的 id,集合的查找时间为 O(1),因此检查 id 是否在此集合中很快。 Then iterate over array_1
and check if the id is present in the created set using has()
.然后迭代
array_1
并使用has()
检查创建的集合中是否存在 id。
let array_1 = [ { id: 1, name: 'Peter' }, { id: 2, name: 'John' }, { id: 3, name: 'Andrew' }, { id: 4, name: 'Patrick' }, { id: 5, name: 'Brian' } ]; let array_2 = [ { id: 1, name: 'not Peter' }, { id: 80, name: 'not John' }, { id: 3, name: 'not Andrew' }, { id: 40, name: 'not Patrick' }, { id: 5, name: 'not Brian' } ]; const array_2_ids = new Set(array_2.map(item_2 => item_2.id)); array_1.forEach(item_1 => item_1.matches = array_2_ids.has(item_1.id)); console.log('matched_array', array_1);
Your current code doesn't work because the for-loop will update the item_1.matches
property for each element in array_2
.您当前的代码不起作用,因为for循环会更新
item_1.matches
在每个元素属性array_2
。 This means you are overwriting the property each time.这意味着您每次都覆盖该属性。 This in turn will effectivly result in
item_1
only being checked against the last item in array_2
.这反过来将有效地导致
item_1
仅根据item_1
中的最后一项进行array_2
。
To make your code work this:为了使您的代码工作:
array_1.forEach(item_1 => { for (let i = 0; i < array_2.length; i++) { item_1.matches = array_2[i].id === item_1.id } });
Should be changed into this:应该改成这样:
array_1.forEach(item_1 => {
for (let i = 0; i < array_2.length; i++) {
if (array_2[i].id === item_1.id) {
item_1.matches = true;
return;
}
}
item_1.matches = false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.