gpt4 book ai didi

javascript - 当存在具有特定名称的键时,数组将对象推送到数组中

转载 作者:太空宇宙 更新时间:2023-11-04 03:02:05 24 4
gpt4 key购买 nike

我有一个如下所示的对象数组

[
{
"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 或 underscore 提供了很好的功能来最小化代码,但这些库仍然没有帮助我解决上述问题

我想实现如下所示的目标

[
{
"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"
]
}

} ]

在我们的例子中,上面的结果是通过将具有相同键名(药物)的对象数组分组,并将其所有值一起获取并将其合并到数组中的单个对象中而形成的。我尝试了不同的方法,但失败了,如果有人可以尝试一下以获得超出预期的输出,我们将不胜感激

提前致谢

最佳答案

这是针对您所面临问题的 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);

关于javascript - 当存在具有特定名称的键时,数组将对象推送到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52270768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com