比较2个数组并更改属性值

时间:2019-01-21 15:49:16

标签: javascript

有2个数组refArr和finalArr。我需要基于refArr更改finalArr中“ external”的值。如果refArr中“ external”的值为true,则finalArr中“ external”的值应更改为true,或者如果refArr中“ external”的值为false,则finalArr中的值应更改为false。在finalArr中,如果“ Editable”的值为false,则表示该对象未被触摸。

let refArr = [
    {number: "8088",external: true,Editable: true, type: "Abcd"},
    {number: "8089",external: true,Editable: true,  type: "efg"},
    {number: "8080",external: false,Editable: true,  type: "hij"}
]

let finalArr = [
    {number: "6543",external: false,Editable: false, type: "xyz"},
    {number: "8089",external: false,Editable: true, type: "efg"},
    {number: "8080",external: true,Editable: true, type: "hij"},
    {number: "8088",external: false,Editable: true, type: "Abcd"},
    {number: "8088",external: false,Editable: true, type: "Abcd"}
]

this is the final output in need 

finalArr = [
    {number: "6543",external: false,Editable: false, type: "xyz"},
    {number: "8089",external: true,Editable: true, type: "efg"},
    {number: "8080",external: false,Editable: true, type: "hij"},
    {number: "8088",external: true,Editable: true, type: "Abcd"},
    {number: "8088",external: true,Editable: true, type: "Abcd"}
]

我无法获得基于refArr更改finalArr中的值的逻辑。

5 个答案:

答案 0 :(得分:2)

或者这个老派的嵌套循环...

let refArr = [
    {number: "8088",external: true,Editable: true, type: "Abcd"},
    {number: "8089",external: true,Editable: true,  type: "efg"},
    {number: "8080",external: false,Editable: true,  type: "hij"}
]

let finalArr = [
    {number: "6543",external: false,Editable: false, type: "xyz"},
    {number: "8089",external: false,Editable: true, type: "efg"},
    {number: "8080",external: true,Editable: true, type: "hij"},
    {number: "8088",external: false,Editable: true, type: "Abcd"},
    {number: "8088",external: false,Editable: true, type: "Abcd"}
]

for(let i = 0; i < finalArr.length; i++) {
    for(let j = 0; j < refArr.length; j++) {
        if(finalArr[i].number === refArr[j].number) {
            finalArr[i].external = refArr[j].external
        }
    }
}
console.log(finalArr)

答案 1 :(得分:1)

不变异原始对象,也不引用原始数组中的对象。

使用Array#map,Array#find,解构和散布语法。

const ref=[{number:"8088",external:!0,Editable:!0,type:"Abcd"},{number:"8089",external:!0,Editable:!0,type:"efg"},{number:"8080",external:!1,Editable:!0,type:"hij"}]
const data=[{number:"6543",external:!1,Editable:!1,type:"xyz"},{number:"8089",external:!1,Editable:!0,type:"efg"},{number:"8080",external:!0,Editable:!0,type:"hij"},{number:"8088",external:!1,Editable:!0,type:"Abcd"},{number:"8088",external:!1,Editable:!0,type:"Abcd"}];

const res = data.map(({external, ...o})=>{
   const r = ref.find(({number})=>number===o.number);
   if(r && r.Editable){
      return {...o, external: r.external}
   }
   return {...o, external}
});

console.log(res);

答案 2 :(得分:0)

let refArr = [
    {number: "8088",external: true,Editable: true, type: "Abcd"},
    {number: "8089",external: true,Editable: true,  type: "efg"},
    {number: "8080",external: false,Editable: true,  type: "hij"}
]

let finalArr = [
    {number: "6543",external: false,Editable: false, type: "xyz"},
    {number: "8089",external: false,Editable: true, type: "efg"},
    {number: "8080",external: true,Editable: true, type: "hij"},
    {number: "8088",external: false,Editable: true, type: "Abcd"},
    {number: "8088",external: false,Editable: true, type: "Abcd"}
]

finalArr.forEach(x => {
    if(!x.Editable) return;
    const ref = refArr.find(y => y.number === x.number);
    if(!ref) return;
    
    if(ref.external === true) x.external = !x.external;
})

console.log(finalArr)

答案 3 :(得分:0)

let refArr = [
    {number: "8088",external: true,Editable: true, type: "Abcd"},
    {number: "8089",external: true,Editable: true,  type: "efg"},
    {number: "8080",external: false,Editable: true,  type: "hij"}
]

let finalArr = [
    {number: "6543",external: false,Editable: false, type: "xyz"},
    {number: "8089",external: false,Editable: true, type: "efg"},
    {number: "8080",external: true,Editable: true, type: "hij"},
    {number: "8088",external: false,Editable: true, type: "Abcd"},
    {number: "8088",external: false,Editable: true, type: "Abcd"}
]

let result = finalArr.map((f) => {
    if (f.Editable) {
        const ref = refArr.find(r => r.number == f.number);
        f.external = ref.external;
    }

    return f;
});

console.log(result);

答案 4 :(得分:0)

如果您不想变异原始的finalArr

const tmp = finalArr.map( x => {
  const match = refArr.find( y => y.number === x.number);

  if ((x.Editable && match) && x.external !== match.external) {
      x.external = match.external;
    }

  return x;
});

如果您不在意finalArr是否被突变:

finalArr.forEach( x => {
  const match = refArr.find( y => y.number === x.number);

  if ((x.Editable && match) && x.external !== match.external) {
      x.external = match.external;
  }  
});