-1

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"
    }
]
Aung Myat Hein
  • 4,018
  • 1
  • 36
  • 42
  • 1
    You should try to use reduce on your array. [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) – Quinox Jul 28 '19 at 10:42
  • @quino I tried to filter and reduce array but I didn't get what I want – Ilias haddad Jul 28 '19 at 10:53

1 Answers1

5

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()])
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • 2
    Nice! Could you please explain the meaning of `let {fileName,createt_at} = inp`? – Reza Saadati Jul 28 '19 at 11:08
  • 2
    @RezaSaadati thanks, this is called [`destructuring`](https://stackoverflow.com/questions/54605286/what-is-destructuring-assignment-and-its-uses), i am using this to take `fileName and createt_at` properties form object – Code Maniac Jul 28 '19 at 11:10
  • Great, I din't know about that. Thank you! – Reza Saadati Jul 28 '19 at 12:39