从阵列中删除重复的数据

时间:2019-10-14 16:54:56

标签: ios arrays react-native filter

我有2个数组。

const finalArr = [] 

const arr = ["abc","def"] 
const arr2 = [ 
               { name: "abc", refresh: false },
               { name: "efd", refresh: false },
               { name: "def", refresh: false } 
             ]

现在我正尝试将arr2中的refresh值更新为true,因为arr中的名称匹配,它会返回重复值:

这是我的代码:

arr2.map(obj => {
      arr.map(name => {

        if (obj.name === name){
          finalArr.push({ ...obj, refresh: true }) 
        } else{
          finalArr.push({ ...obj, refresh: false }) 
        }
      })

有人可以告诉我这是怎么回事吗?

1 个答案:

答案 0 :(得分:3)

这很简单:

const finalArray = arr2.map(obj => {
   return { ...obj, refresh: arr.includes(obj.name) };
}