简体   繁体   English

在数组中设置对象属性 true/false,无论 id 是否与另一个对象数组中的任何 id 匹配

[英]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.

相关问题 有2个具有相同id的对象数组,但一个对象属性为真,另一个属性为假,如果属性为假,如何删除两者 - There is 2 array of object with same id, but one object property is true and other property is false, how to remove both if property is false 使用具有ng-repeat的对象数组中的另一个对象的id属性,通过id查找对象 - Find object by id using another object's id property from array of objects with ng-repeat 检查一个数组是否包含一个 id 与 id 列表匹配的对象 - Check, whether an array contains an object with id that matches list of ids 将一个数组中具有相同 id 的所有对象添加到另一个数组中具有相同 id 的 object 的数组中 - Add all objects with the same id from one array into an array of an object with the same id in another array 将元素与另一个数组匹配的数组的过滤器对象(具有键“id”) - Filter Object of Array (having key 'id') that matches element with another array 如果它与 id 匹配,如何在 object 中的 object 数组中添加一个属性 - how to add a property in object inside array of object if it matches the id 排序对象数组以首先放置与 id 匹配的特定 object - sort array of objects to place a particular object first that matches an id 使用数组方法根据具有相同 id 的另一个数组的元素的存在设置数组元素 object 属性 - set array element object property based on the presence of an element of another array with the same id using array methods 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 如果属性与另一个数组匹配,则检索数组中的对象 - Retrieve objects in array if property matches another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM