How to merge duplicate values in array of objects

Ilias haddad

I have an array of objects and I have duplicated value on it and I want to merge all duration who have been created in the same date and who have the same file Name

Thank You In Advance :)

I have array of objects

[
    {
    fileName: "test.js"
    duration: 1500,
    createt_at: "2019-07-26T09:57:28.126Z"
    },
    {
    fileName: "test.js"
    duration: 1500,
    createt_at: "2019-07-26T10:00:28.126Z"
    },
    {
    fileName: "test.js"
    duration: 1500,
    createt_at: "2019-07-26T09:57:28.126Z"
    },
    {
    fileName: "main.js"
    duration: 1500,
    createt_at: "2019-07-26T09:57:28.126Z"
    },
    {
    fileName: "main.js"
    duration: 3000,
    createt_at: "2019-07-28T10:57:28.126Z"
    },
    {
    fileName: "main.js"
    duration: 2000,
    createt_at: "2019-07-28T11:00:28.126Z"
    }
    {
    fileName: "main.js"
    duration: 100,
    createt_at: "2019-07-28T09:00:28.126Z"
    }
]

Output Array Of Objects Need To Be Like This :

[
    {
    fileName: "test.js"
    duration: 3000,
    createt_at: "2019-07-26"
    },
    {
    fileName: "main.js"
    duration: 1500,
    createt_at: "2019-07-26"
    },
    {
    fileName: "main.js"
    duration: 5100,
    createt_at: "2019-07-28"
    }
]
Code Maniac

You can use fileName and date as combined key and add duration accordingly

  • Loop through array, check if combined key is present on Map or not
  • If not present initialize it with current element's value,
  • If already present then add duration of current element with the value of particular key

let data = [{fileName: "test.js",duration: 1500,createt_at: "2019-07-26T09:57:28.126Z"},{fileName: "test.js",duration: 1500,createt_at: "2019-07-26T10:00:28.126Z"},{fileName: "test.js",duration: 1500,createt_at: "2019-07-26T09:57:28.126Z"},{fileName: "main.js",duration: 1500,createt_at: "2019-07-26T09:57:28.126Z"},{fileName: "main.js",duration: 3000,createt_at: "2019-07-28T10:57:28.126Z"},{fileName: "main.js",duration: 2000,createt_at: "2019-07-28T11:00:28.126Z"}, {fileName: "main.js",duration: 100,createt_at: "2019-07-28T09:00:28.126Z"}]


let op = data.reduce((op,inp) => {
  let {fileName,createt_at} = inp
  let date = createt_at.substring(0,10)
  let key = fileName+date
  if(op.has(key)){
    let val = op.get(key)
    val.duration += inp.duration
  } else {
    op.set(key,inp)
  }
  return op
},new Map())

console.log([...op.values()])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Merge duplicate objects in array of objects

Merge duplicate values in array

How to merge objects and sum just some values of duplicate objects?

Merge properties of objects within a array together using values and remove duplicate

How to delete duplicate values of objects inside array?

How to remove both duplicate values in an array of objects

How to get duplicate values in an array of objects

How to merge values within array of objects

How to merge/combine array of objects based on duplicate keys?

How to merge duplicate array?

Merge values of array by duplicate keys

Remove Duplicate Array and Merge The Values

How to merge duplicate object values in an array javascript based on a name

How to merge duplicate data in a single array/dictionary and return values?

VBA: How to merge duplicate data in a single array/dictionary and return values?

Grouping duplicate objects into an array of values

TS: how to merge values in an array of objects into one array

How to merge and search duplicate values?

Merge and Add values of objects in an array

How to remove duplicate data values from an array of objects in javascript?

How can I check if the array of objects have duplicate property values?

How to get array of objects with no duplicate values by adding 1 using javascript

How can I merge two array of objects and concatenate values?

Merge multidimensional array in php with duplicate values

Merge specific fields of array indexes with duplicate values

Merge 2 objects in an array where the values are an array

Merge two array values into one array of objects

Merge array of objects with array values into a single object

Remove duplicate objects from an array but merge nested objects