Remove original and duplicate from an array of objects - JS

前端 未结 8 1695
时光取名叫无心
时光取名叫无心 2021-01-28 10:40

I have an array of objects.

const arr = [
  { title: \"sky\", artist: \"Jon\", id: 1 },
  { title: \"rain\", artist: \"Paul\", id: 2 },
  { title: \"sky\", artis         


        
8条回答
  •  梦谈多话
    2021-01-28 11:03

    One way to do it, in order to avoid running an exponential loop is to save all values to a new object, and convert that object to a new array.

    const combinedObj = arr.reduce((obj, item) => { obj[item.id] = item; return obj; }, {});
    const newArray = Object.values(combinedObj)
    

提交回复
热议问题