js合併兩個陣列物件

abcByme發表於2024-06-27

來自:https://www.cnblogs.com/keleyz/p/10118648.html 侵刪

let d = [];
            let d2 = [];
            result3[1].forEach((item, index) => {
              d.push({
                name: item,
              });
            });
            result3[2].forEach((item, index) => {
              d2.push({
                num: item,
              });
            });
            this.datacloud = this.merge(d,d2,'num')
merge(a, b, attrName) {
      let attr = attrName;
      b.map((value, index) => {
        //陣列b 長度大於a的長度時,需要判斷
        if (a[index] !== undefined) {
          a[index][attr] = value[attr];
        } else {
          return;
        }
      });
      return a;
    },

相關文章