我有一个像下面这样的对象数组
[
{
"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或下划线提供了使代码最小化的强大功能,但是这些库仍然无法帮助我解决上述问题
我想实现以下目标
[
{
"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"
]
}
} ]
在我们的案例中,通过将具有相同键名(Medication)的对象数组分组,然后将其所有值都提取到一起并将其合并到数组中的单个对象中,形成了以上结果。我尝试了不同的方法,但失败了,如果有人可以尝试获得超出预期的输出,将不胜感激
预先感谢
答案 0 :(得分:2)
这是您面临的问题的O(n)
解决方案。另外,对数组使用 spread语法,这样,如果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);
答案 1 :(得分:2)
您可以使用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; }