How to merge objects in js array

var list= [{id:1,number:2,title:2}, {id:1,number:3,title:2}]
if the values of id and title in the array are the same, and the two objects in the array are merged and the numbered values are added, how to achieve
and finally get [{id:1,number:5,title:2}]

Mar.13,2021

var list=[{id:1,number:2,title:2},{id:1,number:3,title:2}]
function merge (list) {
  let result = []
  let cache = {}
  list.forEach(item => {
    let key = `id:${item.id},title${item.title}`
    let index = cache[key]
    if (index !== undefined) {
      result[index].number += item.number
    } else {
      result.push(Object.assign({}, item))
      cache[key] = result.length - 1
    }
  })
  return result
}

merge(list)

lodash cloneDeep method

Menu