简体   繁体   English

使用地图查找和功能覆盖数组中的对象

[英]Overwrite objects from arrays with map find and function

People, 人,

I´m practicing .map and .find. 我正在练习.map和.find。 I want to map arr1 and overwrite every object from this (arr1) with arr2 when the id matches. 我想映射arr1并在id匹配时用arr2覆盖此(arr1)的每个对象。

<script>

const arr1 = [
  {id: '1', name: 'A', surName: 'A', age: 30},
  {id: '2', name: 'B', surName: 'B', age: 40},
  {id: '3', name: 'C', surName: 'C', age: 50}
];

const arr2 = [
  {id: '1', name: 'D', surName: 'D', age: 60, nickName: 'DD'},
  {id: '5', name: 'E', surName: 'E', age: 70, nickName: 'EE'},
  {id: '3', name: 'F', surName: 'F', age: 80, nickName: 'FF'}
];


let newArr = arr1.map(obj => arr2.find(o => o.id === obj.id) || obj);

console.log(newArr);

</script>

Result: 结果:

0: {…}
age: 60
id: "1"
name: "D"
nickName: "DD"
surName: "D"

1: {…}
age: 40
id: "2"
name: "B"
surName: "B"

2: {…}
age: 80
id: "3"
name: "F"
nickName: "FF"
surName: "F"

However, I would like to add the property nickName with value 'none' for unmatched ones like 但是,我想为不匹配的属性添加值为“ none”的属性nickName

1: {…}
age: 40
id: "2"
name: "B"
surName: "B"

for the resultant array: newArr 对于结果数组:newArr

And I´m really stucked. 而且我真的很困。 I cannot find the way. 我找不到路。 As appremtice I will appreciate help. 感激不尽,我将不胜感激。 Thanks a lot 非常感谢

You can use coalescing to assign the default value of nickName when ID doesn't exist in another array. ID在另一个数组中不存在时,可以使用coalescing分配nickName的默认值。

 const arr1 = [{id: '1', name: 'A', surName: 'A', age: 30},{id: '2', name: 'B', surName: 'B', age: 40},{id: '3', name: 'C', surName: 'C', age: 50}], arr2 = [{id: '1', name: 'D', surName: 'D', age: 60, nickName: 'DD'},{id: '5', name: 'E', surName: 'E', age: 70, nickName: 'EE'},{id: '3', name: 'F', surName: 'F', age: 80, nickName: 'FF'}]; var result = arr1.map(o => { var {nickName} = arr2.find(({id}) => id === o.id) || {nickName: 'none'}; return Object.assign({}, o, {nickName}); }); console.log(result); 

for set nickName = none you need change the || 对于设置nickName = none您需要更改|| for && . 对于&& That's because in that operation you're asking about if those elements have a match or (||) the element obj exist. 这是因为在该操作中,您正在询问这些元素是否匹配,或(||)元素obj存在。 So, you need to change it or all your elements will pass the statements. 因此,您需要对其进行更改,否则所有元素都将通过该语句。 I tried it doing something like this. 我尝试这样做。

let newArr = arr1.map(obj => {
  let x = arr2.find(o => obj && (o.id === obj.id));

  if (x != null) {
    return x;
  }

  obj.nickName = 'none';
  return obj;

  }
);

 const arr1 = [ {id: '1', name: 'A', surName: 'A', age: 30}, {id: '2', name: 'B', surName: 'B', age: 40}, {id: '3', name: 'C', surName: 'C', age: 50} ]; const arr2 = [ {id: '1', name: 'D', surName: 'D', age: 60, nickName: 'DD'}, {id: '5', name: 'E', surName: 'E', age: 70, nickName: 'EE'}, {id: '3', name: 'F', surName: 'F', age: 80, nickName: 'FF'} ]; let newArr = arr1.map(obj => { const match = arr2.find(o => o.id === obj.id) return { ...obj, nickName: match ? match.nickName : 'none' } }); console.log(newArr); 
 <script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script> 

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

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