简体   繁体   English

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

[英]JS -Append key to matching array, object values

I would like to push key values to objects in array1 from other objects of array2 我想将键值从array2的其他对象推到array1中的对象

To do so it needs to search a corresponding values in both arrays, then push the right key. 为此,它需要在两个数组中搜索相应的值,然后按右键。

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"
  }
]

The function need to loop through all objects in array1, look at the first 9 characters of Ref values, find a match in array2 Ref (only first 9 characters are identical). 该函数需要遍历array1中的所有对象,查看Ref值的前9个字符,在array2 Ref中查找匹配项(仅前9个字符相同)。 When there is a match push the "Style" from array2 into the corresponding object in array1 匹配时,将“样式”从array2推入array1中的相应对象

I tried with Object.key.foreach(), map(), with substr to get only 9 characters, with find()... all of this has been a big mess and not working... 我尝试使用Object.key.foreach(),map(),substr来仅获得9个字符,使用find()...所有这些都非常混乱并且无法正常工作...

Expected result : 预期结果 :

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"
  }
]

Assuming those properties are all meant to be Ref (some are Global_Style ), you can use forEach and find : 假设所有这些属性都是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); 

If you didn't want to mutate the array go with map : 如果您不想改变数组,请使用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); 

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));

Something like this may be what you want: 您可能想要这样的东西:

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;
        };
    }
 }

So we loop through the fist array and for each item we loop through the second array. 因此,我们遍历第一个数组,对于每个项目,我们遍历第二个数组。

Then we check if the expected fields are present and if so we compare them. 然后,我们检查期望的字段是否存在,如果存在,我们将它们进行比较。 If they match we add the "Style" field and move to the next object 如果它们匹配,我们添加“样式”字段并移至下一个对象

The Below code will work although we might be able to optimize it further. 以下代码将起作用,尽管我们可以进一步对其进行优化。

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)
      }
  }
}

The first solution is a bit complex. 第一个解决方案有点复杂。 You probable have a typo in array1 as your first key is not consistent. 您可能在array1输入错误,因为您的第一个键不一致。 instead of Global_Stylecode you probably meant Ref , Anyway most likely it should have the same key. 而不是Global_Stylecode您可能的意思是Ref ,无论如何,它应该具有相同的键。 If we assume that the key is Ref, then 如果我们假设键为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;
        }
      })
    });

Also there is no need to define arrays as let . 同样也不需要将数组定义为let const will be fine. const会没事的。

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

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