使用lodash或純js把數(shù)組對象中相同的字段的值合并

1. lodash解決辦法

1.1 數(shù)組內(nèi)對象型數(shù)據(jù)
let fruitSamples = [
      { id: 1, type: 'apples', samples: [{ id: 1, name: 1 }] },
      { id: 2, type: 'bananas', samples: [{ id: 2, name: 2 }] },
      { id: 3, type: 'pears', samples: [{ id: 3, name: 3 }] },
      { id: 1, type: 'apples', samples: [{ id: 11, name: 11 }] },
    ]

    function customizer(objValue, srcValue) {
      if (_.isArray(objValue)) {
        return objValue.concat(srcValue)
      }
    }

    let test = _(fruitSamples)
      .flatten()
      .groupBy('type')
      .map(
        _.spread((...values) => {
          return _.mergeWith(...values, customizer)
        }),
      )
      .value()

console.log(test, 'test')
結果:

[
  { id: 1, type: 'apples', samples: [{ id: 1, name: 1 },{ id: 11, name: 11 }] },
  { id: 2, type: 'bananas', samples: [id: 2, name: 2 }] },
  { id: 3, type: 'pears', samples: [{ id: 3, name: 3 }] }
]
1.2 數(shù)組內(nèi)數(shù)組內(nèi)對象型數(shù)據(jù)
let fruitSamples = [
  [
    {'id': 1,'type': 'apples','samples': [1, 2, 3]},
    {'id': 2,'type': 'bananas','samples': [1, 2, 7]},
    {'id': 3,'type': 'pears','samples': [1, 2, 3]}
  ],
  [
    {'id': 1,'type': 'apples','samples': [5, 2, 9]},
    {'id': 2,'type': 'bananas','samples': [1, 7, 7]},
    {'id': 3,'type': 'pears','samples': [12, 21, 32]}
  ],
  [
    {'id': 1,'type': 'apples','samples': [11, 2, 33]},
    {'id': 2,'type': 'bananas','samples': [17, 2, 67]},
    {'id': 3,'type': 'pears','samples': [91, 22, 34]}
  ]
];

function customizer(objValue, srcValue) {
  if (_.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
}

let test = _(fruitSamples)
  .flatten()
  .groupBy('type')
  .map(_.spread((...values) => {
    return _.mergeWith(...values, customizer);
  }))
  .value();
  
console.log(test);
結果:

[
  { id: 1, type: 'apples', samples: [1, 2, 3, 5, 2, 9, 11, 2, 33] },
  { id: 2, type: 'bananas', samples: [1, 2, 7, 1, 7, 7, 17, 2, 67] },
  { id: 3, type: 'pears', samples: [1, 2, 3, 12, 21, 32, 91, 22, 34] }
]

注:無論samples是單純的數(shù)組內(nèi)string或者數(shù)組內(nèi)number還是數(shù)組內(nèi)Object都支持

2.1 純js處理
let fruitSamples = [
    {
        foo: "A",
        bar: [
            { baz: "1", qux: "a" },
            { baz: "2", qux: "b" }
        ]
    },
    {
        foo: "B",
        bar: [
            { baz: "3", qux: "c" },
            { baz: "4", qux: "d" }
        ]
    },
    {
        foo: "A",
        bar: [
            { baz: "5", qux: "e" },
            { baz: "6", qux: "f" }
        ]
    },
    {
        foo: "B",
        bar: [
            { baz: "7", qux: "g" },
            { baz: "8", qux: "h" }
        ]
    }
]
const hash = Object.create(null)
const test = fruitSamples.filter(function (o) {
  if (!hash[o.foo]) {
     hash[o.foo] = o.bar
     return true
  }
  Array.prototype.push.apply(hash[o.foo], o.bar)
})
console.log(test)
結果:

[
    {
        foo: "A",
        bar: [
            { baz: "1", qux: "a" },
            { baz: "2", qux: "b" },
            { baz: "5", qux: "e" },
            { baz: "6", qux: "f" }
        ]
    },
    {
        foo: "B",
        bar: [
            { baz: "3", qux: "c" },
            { baz: "4", qux: "d" },
            { baz: "7", qux: "g" },
            { baz: "8", qux: "h" }
        ]
    }
]

小伙伴們擇優(yōu)使用

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末雕擂,一起剝皮案震驚了整個濱河市护昧,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖枝冀,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異液走,居然都是意外死亡纲菌,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門宝磨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來弧关,“玉大人,你說我怎么就攤上這事唤锉∈滥遥” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵窿祥,是天一觀的道長株憾。 經(jīng)常有香客問我,道長晒衩,這世上最難降的妖魔是什么号胚? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任假消,我火速辦了婚禮骨田,結果婚禮上,老公的妹妹穿的比我還像新娘涣澡。我一直安慰自己跛锌,他們只是感情好弃秆,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著髓帽,像睡著了一般菠赚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上郑藏,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天衡查,我揣著相機與錄音,去河邊找鬼必盖。 笑死拌牲,一個胖子當著我的面吹牛俱饿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播塌忽,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼拍埠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了土居?” 一聲冷哼從身側響起枣购,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎擦耀,沒想到半個月后棉圈,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡眷蜓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年分瘾,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片账磺。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡芹敌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出垮抗,到底是詐尸還是另有隱情氏捞,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布冒版,位于F島的核電站液茎,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏辞嗡。R本人自食惡果不足惜捆等,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望续室。 院中可真熱鬧栋烤,春花似錦、人聲如沸挺狰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽丰泊。三九已至薯定,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瞳购,已是汗流浹背话侄。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人年堆。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓吞杭,卻偏偏與公主長得像,于是被迫代替她去往敵國和親嘀韧。 傳聞我的和親對象是個殘疾皇子篇亭,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容