简体   繁体   English

如何遍历两个数组并找到公共值对象? 使用 forEach 的 Javascript

[英]How to loop through two arrays and find common value object? Javascript using forEach

I need to looping thought two arrays and find where is value expanded = true set to first array?我需要循环思考两个数组并找到值扩展 = true 设置为第一个数组的位置?

example of first array:第一个数组的例子:

[ 
 { id: 1 , name: 'Test 1' , expended = false },
 { id: 2 , name: 'Test 2' , expended = false },
 { id: 3 , name: 'Test 3' , expended = false }
]

exaple of second array:第二个数组的例子:

[ 
 { id: 1 , name: 'Test 1' , expended = false },
 { id: 2 , name: 'Test 2' , expended = true },
 { id: 3 , name: 'Test 3' , expended = false }
]

Different between two array is only expended value where is on second array is :两个数组之间的不同仅在于第二个数组上的消耗值是:

 { id: 2 , name: 'Test 2' , expended = true }

I need to loop thought first array and second and find in second array where is expended = true and just paste on first array.我需要循环思考第一个数组和第二个数组,然后在第二个数组中找到 expended = true 的位置,然后粘贴到第一个数组上。

The expected result above after the loop should be:循环后上面的预期结果应该是:

first array value第一个数组值

[ 
 { id: 1 , name: 'Test 1' , expended = false },
 { id: 2 , name: 'Test 2' , expended = true },
 { id: 3 , name: 'Test 3' , expended = false }
]

Please, I can't just copy the value of all array!拜托,我不能只复制所有数组的值!

this example is not accepted:不接受此示例:

this.secondArray = this.firstArray.. this.secondArray = this.firstArray..

This is a simple example I sent you.这是我发给你的一个简单例子。 I have 10 more parameters in the object that are changing, but it is only important for me to be expended to copy!我在对象中还有 10 个正在更改的参数,但对我来说只有花费在复制上才重要!

Literally, the value expended from the second array to copy to the first and that's it!从字面上看,从第二个数组中花费的值复制到第一个数组,就是这样!

You can achieve the result using Map and map您可以使用Mapmap实现结果

You can set the value of expended if value of expended of arr1 is false as您可以设置的值expended如果值expendedarr1是假的

arr1.map((o) => ({ ...o, expended: o.expended || map.get(o.id).expended }))

 const arr1 = [ { id: 1, name: "Test 1", expended: false }, { id: 2, name: "Test 2", expended: false }, { id: 3, name: "Test 3", expended: false }, ]; const arr2 = [ { id: 1, name: "Test 1", expended: false }, { id: 2, name: "Test 2", expended: true }, { id: 3, name: "Test 3", expended: false }, ]; const map = new Map(arr2.map((o) => [o.id, o])); const result = arr1.map((o) => ({ ...o, expended: o.expended || map.get(o.id).expended })); console.log(result);
 /* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

If you want the result with forEach如果你想要 forEach 的结果

 const arr1 = [ { id: 1, name: "Test 1", expended: false }, { id: 2, name: "Test 2", expended: false }, { id: 3, name: "Test 3", expended: false }, ]; const arr2 = [ { id: 1, name: "Test 1", expended: false }, { id: 2, name: "Test 2", expended: true }, { id: 3, name: "Test 3", expended: false }, ]; arr1.forEach((obj) => { const elementInArr2 = arr2.find((o) => o.id === obj.id); if (elementInArr2) obj.expended = obj.expended || elementInArr2.expended; }); console.log(arr1);
 /* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

  • Using Array#reduce , iterate over the second array while updating a Map , if the object's expended is true , set the key to the id and the value to the object使用Array#reduce ,在更新Map时迭代第二个数组,如果对象的expendedtrue ,则将键设置为id ,将值设置为对象
  • Using Array#map , iterate over the first array.使用Array#map迭代第一个数组。 For every object check if the above map has its id and replace it, otherwise keep it对于每个对象检查上面的地图是否有它的id并替换它,否则保留它

 let arr1 = [ { id: 1, name: 'Test 1', expended: false }, { id: 2, name: 'Test 2', expended: false }, { id: 3, name: 'Test 3', expended: false } ], arr2 = [ { id: 1, name: 'Test 1', expended: false }, { id: 2, name: 'Test 2', expended: true }, { id: 3, name: 'Test 3', expended: false } ]; const expenededMap = arr2.reduce((map, obj) => { if(obj.expended) map.set(obj.id, obj); return map; }, new Map); arr1 = arr1.map(obj => { if(obj.expended) return obj; const objFromArr2 = expenededMap.get(obj.id) || {}; return objFromArr2.expended ? objFromArr2 : obj; }); console.log(arr1);

Using just Array#forEach and Array#find :只使用Array#forEachArray#find

 let arr1 = [ { id: 1, name: 'Test 1', expended: false }, { id: 2, name: 'Test 2', expended: false }, { id: 3, name: 'Test 3', expended: false } ], arr2 = [ { id: 1, name: 'Test 1', expended: false }, { id: 2, name: 'Test 2', expended: true }, { id: 3, name: 'Test 3', expended: false } ]; arr1.forEach((obj, i, arr) => { if(!obj.expended) { const objFromArr2 = arr2.find(({ id }) => id === obj.id) || {}; if(objFromArr2.expended) arr[i] = objFromArr2; } }); console.log(arr1);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM