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






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




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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages