使用reduce方法的Javascript转换数组
我试图以如下形式变换对象数组:使用reduce方法的Javascript转换数组,javascript,arrays,arraylist,ecmascript-6,Javascript,Arrays,Arraylist,Ecmascript 6,我试图以如下形式变换对象数组: const categories = [{ "category": "Category 1", "desc": "Description 1 of the category 1" }, { "category": "Category 1", "desc": "Descripti
const categories = [{
"category": "Category 1",
"desc": "Description 1 of the category 1"
},
{
"category": "Category 1",
"desc": "Description 2 of the category 1"
}, {
"category": "Category 2",
"desc": "Description 1 of the category 2"
},
{
"category": "Category 2",
"desc": "Description 2 of the category 2"
},
{
"category": "Category 3",
"desc": "Description 1 of the category 3"
}, {
"category": "Category 3",
"desc": "Description 2 of the category 3"
}
];
到表单(所需结果集)
使用reduce
方法:
const result = categories.reduce((acc, item) => {
return [{
...acc,
'category': item.category,
'desriptionList': item.desc
}]
}, [])
但我似乎用错了方法,在错误的地方使用了累加器,因为有一个奇怪的输出
[{
0: {
0: { ... },
category: "Category 3",
desriptionList: "Description 1 of the category 3"
},
category: "Category 3",
desriptionList: "Description 2 of the category 3"
}]
由于需要使用将
desc
分组到类别,因此需要使用对象作为累加器。在每次迭代中,将累加器(acc
)展开到一个新对象中,并添加当前的类别
要创建或更新类别的对象,请添加类别
,并扩展该类别的上一个实例(未定义
),添加描述列表
,并扩展上一个描述列表
(如果没有,则为回退),然后添加当前的描述
项
使用Object.values()
转换为数组
const categories=[{“category”:“category 1”,“desc”:“Description 1 of the category 1”},{“category”:“category 1”,“desc”:“Description 2 of the category 2”},{“category 2”,“desc”:“Description 2 of the category 2”},{“category 3”,“desc“第3类的说明1”},{“类别”:“第3类”,“说明”:“第3类的说明2”}];
const result=Object.values(categories.reduce)(acc,{category,desc})=>({
…acc,
[类别]:{
…acc[类别],
类别
描述列表:[
…(附件[类别]?.描述列表| |[]),
{desc}
]
}
}), {}))
console.log(result)
由于需要使用将desc
分组到类别中,因此需要使用一个对象作为累加器。在每次迭代中,将累加器(acc
)展开到一个新对象中,并添加当前的类别
要创建或更新类别的对象,请添加类别
,并扩展该类别的上一个实例(未定义
),添加描述列表
,并扩展上一个描述列表
(如果没有,则为回退),然后添加当前的描述
项
使用Object.values()
转换为数组
const categories=[{“category”:“category 1”,“desc”:“Description 1 of the category 1”},{“category”:“category 1”,“desc”:“Description 2 of the category 2”},{“category 2”,“desc”:“Description 2 of the category 2”},{“category 3”,“desc类别3的说明1“},{“类别”:“类别3”,“描述”:“类别3的说明2”}];
const result=Object.values(categories.reduce)(acc,{category,desc})=>({
…acc,
[类别]:{
…acc[类别],
类别
描述列表:[
…(附件[类别]?.描述列表| |[]),
{desc}
]
}
}), {}))
console.log(result)
保持简单,并使用Array.reduce()和Array.findIndex迭代每个对象,决定是否应将其数据添加到累加器中的现有对象,或者是否应创建并推送新对象
const categories=[{category:“category 1”,desc:“Description 1 of the category 1”},{category:“category 2”,desc:“Description 1 of the category 2”},{category:“Description 2 of the category 2”},{categority:“Description 1 of the category 3”},{categority:“Description 1 of the categories 3”},{categority:第3类”,说明:“第3类的说明2“}];
常量结果=类别。减少((acc,cur)=>{
设idx=acc.findIndex(obj=>obj.category===cur.category)
如果(idx>-1)
{
acc[idx].descriptionList.push(cur.desc)
}
其他的
{
acc.push({category:cur.category,descriptionList:[cur.desc]})
}
返回acc
}, [])
console.log(result)
保持简单,并使用Array.reduce()和Array.findIndex迭代每个对象,决定是否应将其数据添加到累加器中的现有对象,或者是否应创建并推送新对象
const categories=[{category:“category 1”,desc:“Description 1 of the category 1”},{category:“category 2”,desc:“Description 1 of the category 2”},{category:“Description 2 of the category 2”},{categority:“Description 1 of the category 3”},{categority:“Description 1 of the categories 3”},{categority:第3类”,说明:“第3类的说明2“}];
常量结果=类别。减少((acc,cur)=>{
设idx=acc.findIndex(obj=>obj.category===cur.category)
如果(idx>-1)
{
acc[idx].descriptionList.push(cur.desc)
}
其他的
{
acc.push({category:cur.category,descriptionList:[cur.desc]})
}
返回acc
}, [])
console.log(结果)
!target.descriptionList.includes(curr)”条件确保每个类别都没有重复的描述。如果您觉得不必要,请随意删除它
const categories = [{
"category": "Category 1",
"desc": "Description 1 of the category 1"
},
{
"category": "Category 1",
"desc": "Description 2 of the category 1"
}, {
"category": "Category 2",
"desc": "Description 1 of the category 2"
},
{
"category": "Category 2",
"desc": "Description 2 of the category 2"
},
{
"category": "Category 3",
"desc": "Description 1 of the category 3"
}, {
"category": "Category 3",
"desc": "Description 2 of the category 3"
}
];
const newArr = categories.reduce((acc, curr) => {
let target = acc.find(element => element.category === curr.category);
if (target) {
if (!target.descriptionList.includes(curr)) {
delete curr.category;
target.descriptionList.push(curr);
}
} else {
acc.push({
category: curr.category,
descriptionList: [{ desc: curr.desc }]
})
}
return acc
}, []);
console.log('newArr: ' + JSON.stringify(newArr));
输出
newArr: [
{
"category": "Category 1",
"descriptionList": [
{
"desc": "Description 1 of the category 1"
},
{
"desc": "Description 2 of the category 1"
}
]
},
{
"category": "Category 2",
"descriptionList": [
{
"desc": "Description 1 of the category 2"
},
{
"desc": "Description 2 of the category 2"
}
]
},
{
"category": "Category 3",
"descriptionList": [
{
"desc": "Description 1 of the category 3"
},
{
"desc": "Description 2 of the category 3"
}
]
}
]
“!target.descriptionList.includes(curr)”条件确保每个类别都没有重复的描述。如果您觉得不必要,请随意删除它
const categories = [{
"category": "Category 1",
"desc": "Description 1 of the category 1"
},
{
"category": "Category 1",
"desc": "Description 2 of the category 1"
}, {
"category": "Category 2",
"desc": "Description 1 of the category 2"
},
{
"category": "Category 2",
"desc": "Description 2 of the category 2"
},
{
"category": "Category 3",
"desc": "Description 1 of the category 3"
}, {
"category": "Category 3",
"desc": "Description 2 of the category 3"
}
];
const newArr = categories.reduce((acc, curr) => {
let target = acc.find(element => element.category === curr.category);
if (target) {
if (!target.descriptionList.includes(curr)) {
delete curr.category;
target.descriptionList.push(curr);
}
} else {
acc.push({
category: curr.category,
descriptionList: [{ desc: curr.desc }]
})
}
return acc
}, []);
console.log('newArr: ' + JSON.stringify(newArr));
输出
newArr: [
{
"category": "Category 1",
"descriptionList": [
{
"desc": "Description 1 of the category 1"
},
{
"desc": "Description 2 of the category 1"
}
]
},
{
"category": "Category 2",
"descriptionList": [
{
"desc": "Description 1 of the category 2"
},
{
"desc": "Description 2 of the category 2"
}
]
},
{
"category": "Category 3",
"descriptionList": [
{
"desc": "Description 1 of the category 3"
},
{
"desc": "Description 2 of the category 3"
}
]
}
]