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"
}
]
You can use fileName and date
as combined key and add duration accordingly
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.
Comments