在我的nodeJs代码中,我从数据库中获取一些记录。我将数据作为JSON获取如下:
"Data": [{
"id": 1,
"color": "blue",
"model_name": "ford",
"year": 2016
}, {
"id": 2,
"color": "blue",
"model_name": "Maruti",
"year": 2016
}, {
"id": 3,
"color": "red",
"model_name": "Fiat",
"year": 2016
}, {
"id": 4,
"color": "red",
"model_name": "tata",
"year": 2016
}]
我想要的是类似下面的JSON:
"Data": [{
"color": "blue",
car: [{
"id": 1,
"color": "blue",
"model_name": "ford",
"year": 2016
}, {
"id": 2,
"color": "blue",
"model_name": "Maruti",
"year": 2016
}]
}, {
"color": "red",
car: [{
"id": 3,
"color": "red",
"model_name": "Fiat",
"year": 2016
}, {
"id": 4,
"color": "red",
"model_name": "tata",
"year": 2016
}]
}]
我可以使用以下代码使用下划线JS的'groupBy'方法实现此目的:
var groupedData = _.groupBy(rows, f=>{return f.color});
然而输出变为:
"Data": [{
"blue": [{
"id": 1,
"color": "blue",
"model_name": "ford",
"year": 2016
}, {
"id": 2,
"color": "blue",
"model_name": "Maruti",
"year": 2016
}]
}, {
"red": [{
"id": 3,
"color": "red",
"model_name": "Fiat",
"year": 2016
}, {
"id": 4,
"color": "red",
"model_name": "tata",
"year": 2016
}]
}]
问题在于颜色名称本身就是关键。我想要这种格式的“颜色”:“蓝色”,但是在一组中。
答案 0 :(得分:2)
在这里,我做了一个小groupBy
函数,适用于你的情况。为了保持通用性,我更换了汽车"关键名称" items" :
function groupBy(key, array) {
var result = [];
for (var i = 0; i < array.length; i++) {
var added = false;
for (var j = 0; j < result.length; j++) {
if (result[j][key] == array[i][key]) {
result[j].items.push(array[i]);
added = true;
break;
}
}
if (!added) {
var entry = {items: []};
entry[key] = array[i][key];
entry.items.push(array[i]);
result.push(entry);
}
}
return result;
}
现在如果
var data = [{
"id": 1,
"color": "blue",
"model_name": "ford",
"year": 2016
}, {
"id": 2,
"color": "blue",
"model_name": "Maruti",
"year": 2016
}, {
"id": 3,
"color": "red",
"model_name": "Fiat",
"year": 2016
}, {
"id": 4,
"color": "red",
"model_name": "tata",
"year": 2016
}]
然后
groupBy("color", data);
应该给你预期的结果:
[
{
"color": "blue",
"items": [
{
"id": 1,
"color": "blue",
"model_name": "ford",
"year": 2016
},
{
"id": 2,
"color": "blue",
"model_name": "Maruti",
"year": 2016
}
]
},
{
"color": "red",
"items": [
{
"id": 3,
"color": "red",
"model_name": "Fiat",
"year": 2016
},
{
"id": 4,
"color": "red",
"model_name": "tata",
"year": 2016
}
]
}
]
答案 1 :(得分:2)
以下是使用lodash的简短回答:
...where now() between start_date and iif( end_date IS NULL, #2099-12-31 23:59:59#, end_date )
如果您发现令人困惑,可以将其视为:
_(Data)
.groupBy((elem) => elem.color)
.map((vals, key) => ({color: key, car: vals}))
.value()
希望这有帮助。
答案 2 :(得分:1)
const data = [
{
"id": 1,
"color": "blue",
"model_name": "ford",
"year": 2016
},
{
"id": 2,
"color": "blue",
"model_name": "Maruti",
"year": 2016
},
{
"id": 3,
"color": "red",
"model_name": "Fiat",
"year": 2016
},
{
"id": 4,
"color": "red",
"model_name": "tata",
"year": 2016
}
];
const sorted = data.reduce((result, car) => {
const a = result.find(({color}) => color === car.color);
a ? a.car.push(car) : result.push({color: car.color, car: [car]});
return result;
}, []);
console.log(sorted);
答案 3 :(得分:1)
纯ES6代码的另一种直接方式。
var data = [
{
"id": 1,
"color": "blue",
"model_name": "ford",
"year": 2016
},
{
"id": 2,
"color": "blue",
"model_name": "Maruti",
"year": 2016
},
{
"id": 3,
"color": "red",
"model_name": "Fiat",
"year": 2016
},
{
"id": 4,
"color": "red",
"model_name": "tata",
"year": 2016
}],
hash = data.reduce((p,c) => (p[c.color] ? p[c.color].push(c) : p[c.color] = [c],p) ,{}),
newData = Object.keys(hash).map(k => ({color: k, car: hash[k]}));
console.log(newData);
&#13;