收到的JSON响应
[{
"name": "A1",
"date": "2019-03-13",
"comment": "xyz"
},
{
"name": "A1",
"date": "2019-03-13",
"comment": "abc"
},
{
"name": "B1",
"date": "2019-03-13",
"comment": "pqr"
},
{
"name": "A1",
"date": "2019-03-14",
"comment": "mno"
}
]
预期产量
[
{
"name": "A1",
"date": "2019-03-13",
"data": [
{
"name": "A1",
"date": "2019-03-13",
"comment": "xyz"
},
{
"name": "A1",
"date": "2019-03-13",
"comment": "abc"
}
],
{
"name": "A1",
"date": "2019-03-14",
"data": [
{
"name": "A1",
"date": "2019-03-14",
"comment": "mno"
}
]
},
{
"name": "B1",
"date": "2019-03-13",
"data": [
{
"name": "B1",
"date": "2019-03-13",
"comment": "pqr"
}
]
}
]
我想创建一个新的javascript数组,其中包含唯一的名称和日期及其各自的数据。
有人可以在这里帮助我吗?
答案 0 :(得分:3)
您可以使用array#reduce
在对象累加器中根据name
和date
对日期进行分组,然后使用Object.values()
提取所有值。
let data = [{ "name": "A1", "date": "2019-03-13", "comment": "xyz" }, { "name": "A1", "date": "2019-03-13", "comment": "abc" }, { "name": "B1", "date": "2019-03-13", "comment": "pqr" }, { "name": "A1", "date": "2019-03-14", "comment": "mno" } ],
result = Object.values(data.reduce((r,{name,date, comment}) => {
r[name + "_" + date] = r[name + "_" + date] || {name, date, data: []};
r[name + "_" + date].data.push({name,date,comment});
return r;
},{}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:3)
您可以为所需的分组取一个数组,然后查找对象。
var data = [{ name: "A1", date: "2019-03-13", comment: "xyz" }, { name: "A1", date: "2019-03-13", comment: "abc" }, { name: "B1", date: "2019-03-13", comment: "pqr" }, { name: "A1", date: "2019-03-14", comment: "mno" }],
groupBy = ['name', 'date'],
grouped = data.reduce((r, o) => {
var temp = r.find(p => groupBy.every(k => o[k] === p[k]));
if (!temp) {
r.push(temp = Object.assign(...groupBy.map(k => ({ [k]: o[k] })), { data: [] }));
}
temp.data.push(o);
return r;
}, []);
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:2)
您可以使用reduce
来收集值:
var input = [{
"name": "A1",
"date": "2019-03-13",
"comment": "xyz"
},
{
"name": "A1",
"date": "2019-03-13",
"comment": "abc"
},
{
"name": "B1",
"date": "2019-03-13",
"comment": "pqr"
},
{
"name": "A1",
"date": "2019-03-14",
"comment": "mno"
}
]
var res = input.reduce((acc, {name, date, comment}) => {
var found = acc.find(el => el.name === name && el.date === date);
return found
? found.data.push({name, date, comment}) && acc
: [...acc, {name, date, data: [{name, date, comment}]}];
}, []);
console.log(JSON.stringify(res));
答案 3 :(得分:0)
一种简便方法:
const data = [{
"name": "A1",
"date": "2019-03-13",
"comment": "xyz"
},
{
"name": "A1",
"date": "2019-03-13",
"comment": "abc"
},
{
"name": "A1",
"date": "2019-03-13",
"comment": "fffffffff"
},
{
"name": "B1",
"date": "2019-03-13",
"comment": "pqr"
},
{
"name": "B1",
"date": "2019-03-13",
"comment": "hhhhhhhhhhh"
},
{
"name": "A1",
"date": "2019-03-14",
"comment": "mno"
}
]
const res = data.reduce((all, acc) => {
const found = all.find(o => o.name === acc.name && o.date === acc.date)
if (found === undefined) {
all.push(acc)
} else {
found.data = (found.data || []).concat(acc)
}
return all;
}, [])
console.log(res)