Last active
April 6, 2022 18:50
-
-
Save Bengejd/3cb38eec234466ef79f97d5012df0be9 to your computer and use it in GitHub Desktop.
Reduce & Spread an array of object values to a singular object with mapped [keyValue]: newValue pairs instead of [key]: value pairs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dataMap = (data, valueArgs) => { | |
return data | |
.map( (obj) => ({ | |
date: new Date(obj.date), | |
manual: true, | |
...valueArgs.reduce((prev, curr, index) => ({ | |
...prev, | |
[curr.name]: curr.split | |
? Number(obj.value.split("/")[index]) | |
: Number(obj.value), | |
}), {}), | |
})); | |
}; | |
const noArgsData = [ | |
{ | |
date: "2020-01-31", | |
value: "110", | |
}, | |
{ | |
date: "2020-05-15", | |
value: "80", | |
}, | |
]; | |
const noArgsArr = []; | |
const noArgsExample = dataMap(noArgsData, noArgsArr); | |
console.log(noArgsExample); | |
/* | |
[{ | |
date: [object Date] { ... }, | |
manual: true | |
}, { | |
date: [object Date] { ... }, | |
manual: true | |
}] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Real world example */ | |
const dataMap = (data, valueArgs) => { | |
return data | |
.map( (obj) => ({ | |
date: new Date(obj.date), | |
manual: true, | |
...valueArgs.reduce((prev, curr, index) => ({ | |
...prev, | |
[curr.name]: curr.split | |
? Number(obj.value.split("/")[index]) | |
: Number(obj.value), | |
}), {}), | |
})); | |
}; | |
const noSplitData = [ | |
{ | |
date: "2020-01-31", | |
value: "110", | |
}, | |
{ | |
date: "2020-05-15", | |
value: "80", | |
}, | |
]; | |
const noSplitArgsArr = [ { name: "latest", split: false } ]; | |
const withoutSplit = dataMap(noSplitData, noSplitArgsArr); | |
console.log(withoutSplit); | |
//Output withoutSplit | |
/* | |
[{ | |
date: [object Date] { ... }, | |
latest: 110, | |
manual: true, | |
}, { | |
date: [object Date] { ... }, | |
latest: 80, | |
manual: true, | |
}] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Simple example */ | |
const objArr = [{name: 0}, {name: 1}, {name: 2}]; | |
const val = { | |
...objArr.reduce((prev, curr, index) => ({ ...prev, [curr.name]: index }), {}), | |
"4": 3, | |
"5": 4, | |
"6": 5 | |
}; | |
//Output { 0: 0, 1: 1, 2: 2, 4: 3, 5: 4, 6: 5 } | |
console.log(val); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Real world example */ | |
const dataMap = (data, valueArgs) => { | |
return data | |
.map( (obj) => ({ | |
date: new Date(obj.date), | |
manual: true, | |
...valueArgs.reduce((prev, curr, index) => ({ | |
...prev, | |
[curr.name]: curr.split | |
? Number(obj.value.split("/")[index]) | |
: Number(obj.value), | |
}), {}), | |
})); | |
}; | |
const splitData = [ | |
{ | |
date: "2020-01-31", | |
value: "110/75", | |
}, | |
{ | |
date: "2020-05-15", | |
value: "80/120", | |
}, | |
]; | |
const splitArgsArr = [ { name: "latest", split: true }, { name: "previous", split: true } ]; | |
const withSplit = dataMap(splitData, splitArgsArr); | |
console.log(withSplit); | |
// Output with Split | |
/* | |
[{ | |
date: [object Date] { ... }, | |
latest: 110, | |
manual: true, | |
previous: 75 | |
}, { | |
date: [object Date] { ... }, | |
latest: 80, | |
manual: true, | |
previous: 120 | |
}] | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment