Array push object inside an array when a key with some particular name exists
Array push object inside an array when a key with some particular name exists
I have an array of objects like below
[
"medication":
"name": "Turbohaler",
"time": [
"2018-09-07"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-08"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-09"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-10"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-11"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-07"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-08"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-09"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-10"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-11"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-07"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-08"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-09"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-10"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-11"
]
]
lodash or underscore provided great functions to minimalise the code, but still those libraries didn't help me solve the above problem
I want to achieve something like below
[
"medication":
"name": "Turbohaler",
"time": [
"2018-09-07",
"2018-09-08",
"2018-09-09",
"2018-09-10",
"2018-09-11"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-07",
"2018-09-08",
"2018-09-09",
"2018-09-10",
"2018-09-11"
]
]
The above result is formed by grouping an array of objects with same key name (Medication) in our case, and fetching all its values together and merging it into a single object in an array. I have tried different approaches and it failed, If someone could try a bit for above expected output, it will be much appreciated
Thanks in advance
2 Answers
2
Here is a O(n) solution for the problem you are facing. In addition, use spread syntax on array so that if the time array has multiple items it is handled properly.
O(n)
time
var arr = [
"medication":
"name": "Turbohaler",
"time": [
"2018-09-07"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-08"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-09"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-10"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-11"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-07"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-08"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-09"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-10"
]
,
"medication":
"name": "Turbohaler",
"time": [
"2018-09-11"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-07"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-08"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-09"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-10"
]
,
"medication":
"name": "Septron",
"time": [
"2018-09-11"
]
];
var tempObj = ;
arr.forEach((item)=>
if(!tempObj[item.medication.name])
tempObj[item.medication.name] = item;
else
tempObj[item.medication.name].medication.time.push(...item.medication.time);
);
var res = Object.values(tempObj);
console.log(res);
sounds like a perfect solution, Thanks
– Ashwin S
Sep 11 '18 at 7:37
You could take a Map and then collect all values for a given name and render then a new array.
Map
var data = [ medication: name: "Turbohaler", time: ["2018-09-07"] , medication: name: "Turbohaler", time: ["2018-09-08"] , medication: name: "Turbohaler", time: ["2018-09-09"] , medication: name: "Turbohaler", time: ["2018-09-10"] , medication: name: "Turbohaler", time: ["2018-09-11"] , medication: name: "Turbohaler", time: ["2018-09-07"] , medication: name: "Turbohaler", time: ["2018-09-08"] , medication: name: "Turbohaler", time: ["2018-09-09"] , medication: name: "Turbohaler", time: ["2018-09-10"] , medication: name: "Turbohaler", time: ["2018-09-11"] , medication: name: "Septron", time: ["2018-09-07"] , medication: name: "Septron", time: ["2018-09-08"] , medication: name: "Septron", time: ["2018-09-09"] , medication: name: "Septron", time: ["2018-09-10"] , medication: name: "Septron", time: ["2018-09-11"] ],
grouped = Array.from(
data.reduce((m, medication: name, time ) => m.set(name, (m.get(name) || ).concat(time)), new Map),
([name, time]) => ( medication: name, time )
);
console.log(grouped);
.as-console-wrapper max-height: 100% !important; top: 0;
looks like a simplified approach. Thanks @Nina
– Ashwin S
Sep 11 '18 at 7:54
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Sep 11 '18 at 7:27