JS-将键附加到匹配的数组,对象值

时间:2019-04-09 07:34:30

标签: javascript arrays node.js object

我想将键值从array2的其他对象推到array1中的对象

为此,它需要在两个数组中搜索相应的值,然后按右键。

let array1 = [
  {
    "Ref": "28189-060-B",
    "Otherkey": "Whatever"
  },
  {
    "Ref": "18182-250-B",
    "Otherkey": "Whatever2"
  },
  {
    "Ref": "55187-753-B",
    "Otherkey": "Whatever3"
  }
]

let array2 = [
  {
    "Ref": "28189-060-ABCD",
    "Style": "Red"
  },
  {
    "Ref": "18182-250-ABCD",
    "Style": "Blue"
  },
  {
    "Ref": "55187-753-ABCD",
    "Style": "Yellow"
  }
]

该函数需要遍历array1中的所有对象,查看Ref值的前9个字符,在array2 Ref中查找匹配项(仅前9个字符相同)。匹配时,将“样式”从array2推入array1中的相应对象

我尝试使用Object.key.foreach(),map()和substr来仅获得9个字符,并使用find()...所有这些都是一团糟,无法正常工作...

预期结果:

let array1 = [
    {
    "Ref": "18182-250-B",
    "Otherkey": "Whatever2",
    "Style": "Blue"
  },
{
    "Ref": "28189-060-B",
    "Otherkey": "Whatever",
    "Style": "Red"
  },
  {
    "Ref": "55187-753-B",
    "Otherkey": "Whatever3",
    "Style": "Yellow"
  }
]

5 个答案:

答案 0 :(得分:1)

假设这些属性都是Ref(有些是Global_Style),则可以使用forEachfind

let array1 = [{"Ref":"28189-060-B","Otherkey":"Whatever"},{"Ref":"18182-250-B","Otherkey":"Whatever2"},{"Ref":"55187-753-B","Otherkey":"Whatever3"}];
let array2 = [{"Ref":"28189-060-ABCD","Style":"Red"},{"Ref":"18182-250-ABCD","Style":"Blue"},{"Ref":"55187-753-ABCD","Style":"Yellow"}];

const shorterRef = (ref) => ref.substr(0, 9);

array1.forEach(obj => {
  const a1Ref = shorterRef(obj.Ref);
  const arr2Obj = array2.find(tmp => shorterRef(tmp.Ref) === a1Ref);
  if (arr2Obj) obj.Style = arr2Obj.Style;
});

console.log(array1);

如果您不想更改数组,请使用map

let array1 = [{"Ref":"28189-060-B","Otherkey":"Whatever"},{"Ref":"18182-250-B","Otherkey":"Whatever2"},{"Ref":"55187-753-B","Otherkey":"Whatever3"}];
let array2 = [{"Ref":"28189-060-ABCD","Style":"Red"},{"Ref":"18182-250-ABCD","Style":"Blue"},{"Ref":"55187-753-ABCD","Style":"Yellow"}];

const shorterRef = (ref) => ref.substr(0, 9);

const out = array1.map(obj => {
  const a1Ref = shorterRef(obj.Ref);
  const arr2Obj = array2.find(tmp => shorterRef(tmp.Ref) === a1Ref);
  if (arr2Obj) return { ...obj,  Style: arr2Obj.Style };
});

console.log(out);

答案 1 :(得分:0)

var arrMap = {};
array1.forEach(function(x){
    if(!arrMap[x.Ref.substring(0,9)]){
        arrMap[x.Ref.substring(0,9)] = x;
    }
});

array2.forEach(function(x){
 if(Object.keys(arrMap).includes(x.Ref.substring(0,9))){
        arrMap[x.Ref.substring(0,9)] = Object.assign(arrMap[x.Ref.substring(0,9)], {"Style": x.Style});
    }
});
console.log(Object.values(arrMap));

答案 2 :(得分:0)

您可能想要这样的东西

array1.forEach(function (element1) {
    array2.forEach(function (element2){
        addStyle(element1, element2);        
    });
});

function addStyle(obj1, obj2){
    if (obj1.Ref && obj2.Ref){
        let Ref1 = obj1.Ref.substr(0,8);
        let Ref2 = obj2.Ref.substr(0, 8);
        if (Ref1 === Ref2){
            obj1.Style = obj2.Style;
        };
    }
 }

因此,我们遍历第一个数组,对于每个项目,我们遍历第二个数组。

然后,我们检查期望的字段是否存在,如果存在,我们将它们进行比较。如果它们匹配,我们添加“样式”字段并移至下一个对象

答案 3 :(得分:0)

下面的代码将起作用,尽管我们可以进一步对其进行优化。

var newArr = []

for(let k in array1){
  for(let i in array2){
    console.log(array2[i]['Ref'].substr(0,9))

      if(array1[k]['Ref'].substr(0,9) == array2[i]['Ref'].substr(0,9)){
          let temp = array1[k]
          temp['Style'] = array2[i]['Style']
          newArr.push(temp)
      }
  }
}

答案 4 :(得分:0)

第一个解决方案有点复杂。 您的第一个键不一致,可能是array1中有错字。而不是Global_Stylecode可能意味着Ref,无论如何,它应该具有相同的密钥。如果我们假设键为Ref,那么

    array1.forEach( ({Ref: Ref1, Otherkey}, index) => {
      const Ref1Sub = Ref1.substring(0, 9);
      array2.forEach(({Ref: Ref2, Style}) => {
        if (Ref2.includes(Ref1Sub)) {
          array1[index].Style = Style;
        }
      })
    });

也无需将数组定义为letconst可以。