我有一个json文件:
var records=
[
{
"category" : "chinese",
"name": "noodles"
},
{
"category" : "chinese",
"name": "veg"
},
{
"category" : "Indian",
"name": "Dal"
},
{
"category" : "Indian",
"name": "Rajma"
},
{
"category" : "chinese",
"name": "chicken"
},
{
"category" : "continental",
"name": "fish"
},
{
"category" : "continental",
"name": "Veg"
},
{
"category" : "Indian",
"name": "Paratha"
}
]
我正在尝试将此JSON转换为以下格式:
var newcat =
{
"chinese": [
{ "name": "noodles", "category": "chinese" },
{"name": "veg", "category": "chinese"},
{"name":"chicken", "category": "chinese"}
],
"indian": [
{ "name": "Dal", "category": "indian" },
{"name": "rajma", "category": "indian"},
{"name":"chicken", "category": "indian" }
],
"continental": [
{ "name": "fish", "category": "continental" },
{ "name": "veg", "category": "continental"}
]
}
我在角度中使用的代码是:
var newcat={}
var arr= {}
var isPresent= false;
var cat
for (let i=0;i< records.length;i++) {
cat= records[i].category
for(let item in newcat) {
if(cat == item) {
isPresent= true
}
else {
isPresent= false
}
}
if(isPresent== false){
newcat[cat]= []
}
else {
newcat[cat].push(records[i])
console.log("===>",newcat)
}
}
我能够构建对象,除此之外,每个类别的所有项目都没有输入新对象 newcat 。
结果应该在console.log上显示:
{chinese: Array(3), indian: Array(3), continental: Array(2)}
相反它会像:
{chinese: Array(0), indian: Array(1), continental: Array(1)}
我无法弄清楚为什么所有元素都没有进入
答案 0 :(得分:3)
如果您已经使用Lodash,请使用_.groupBy
。否则,您可以编写自己的groupBy
函数:
var records = [
{
"category" : "chinese",
"name": "noodles"
},
{
"category" : "chinese",
"name": "veg"
},
{
"category" : "Indian",
"name": "Dal"
},
{
"category" : "Indian",
"name": "Rajma"
},
{
"category" : "chinese",
"name": "chicken"
},
{
"category" : "continental",
"name": "fish"
},
{
"category" : "continental",
"name": "Veg"
},
{
"category" : "Indian",
"name": "Paratha"
}
];
function groupBy(array, key) {
return array.reduce((groups, value) => {
const group = groups[value[key]] || (groups[value[key]] = []);
group.push(value);
return groups;
}, {});
}
console.log(groupBy(records, 'category'));
或者,使用for
循环代替reduce
:
function groupBy(array, key) {
const groups = {};
for(let i = 0; i < array.length; i++) {
const value = array[i];
const valueKey = value[key];
if(!groups[valueKey]) groups[valueKey] = [];
groups[valueKey].push(value);
}
return groups;
}
编辑:修复现有代码:
您的实施有两个问题。首先,设置for/in
的{{1}}循环在找到匹配后不会isPresent
。其次,最后的break
语句会导致类别中的第一项不被推送,因为您所做的就是创建空类别数组。这是修复:
else
答案 1 :(得分:1)
如果您使用ecmascript-6作为标记,您应该可以访问传播运算符而不会使用Lodash:
这基本上应该为你完成所有工作:
records.map(item => {
newcat[item.category.toLowerCase()] = newcat[item.category.toLowerCase()] || []
newcat[item.category.toLowerCase()] = [
...newcat[item.category.toLowerCase()],
{ ...item }
]
})
下面的工作代码段:
var records = [{
"category": "chinese",
"name": "noodles"
},
{
"category": "chinese",
"name": "veg"
},
{
"category": "Indian",
"name": "Dal"
},
{
"category": "Indian",
"name": "Rajma"
},
{
"category": "chinese",
"name": "chicken"
},
{
"category": "continental",
"name": "fish"
},
{
"category": "continental",
"name": "Veg"
},
{
"category": "Indian",
"name": "Paratha"
}
]
var newcat = {}
records.map(item => {
newcat[item.category.toLowerCase()] = newcat[item.category.toLowerCase()] || []
newcat[item.category.toLowerCase()] = [
...newcat[item.category.toLowerCase()],
{ ...item }
]
})
console.log(newcat)
&#13;