英文:
Array conversion manipulation and formatting
问题
{
car: ["level3", "level2"],
bike: ["level1", "level5"]
}
英文:
I have the following array:
[
{
"level": "level3",
"category": "car"
},
{
"level": "level1",
"category": "bike"
},
{
"level": "level2",
"category": "car"
},
{
"level": "level5",
"category": "bike"
}
]
I need to convert this to the following object:
{
car: ["level3", "level2"],
bike: ["level1", "level5"],
}
What would be the best way to achieve this? Thanks in advance
答案1
得分: 1
你可以通过多种方法实现你想要的效果:
for...of
方法
const data = [ { "level": "level3", "category": "car" }, { "level": "level1", "category": "bike" }, { "level": "level2", "category": "car" }, { "level": "level5", "category": "bike" } ];
const newObj = {};
for (const item of data) {
const {level,category} = item;
if (newObj[category]) {
newObj[category].push(level);
} else {
newObj[category] = [level];
}
}
console.log(newObj);
reduce
方法
const data = [ { "level": "level3", "category": "car" }, { "level": "level1", "category": "bike" }, { "level": "level2", "category": "car" }, { "level": "level5", "category": "bike" } ];
const newObj = data.reduce((acc, item) => {
const { level, category } = item;
acc[category] = acc[category] || [];
acc[category].push(level);
return acc;
}, {});
console.log(newObj);
第一种方法简单易懂,第二种方法更具功能性。但这两种方法的时间复杂度都是O(n),选择其中一种只是个人偏好和编码风格的问题。
英文:
You could achieve what you want with many approaches:
for...of
approach
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const data = [ { "level": "level3", "category": "car" }, { "level": "level1", "category": "bike" }, { "level": "level2", "category": "car" }, { "level": "level5", "category": "bike" } ];
const newObj = {};
for (const item of data) {
const {level,category} = item;
if (newObj[category]) {
newObj[category].push(level);
} else {
newObj[category] = [level];
}
}
console.log(newObj);
<!-- end snippet -->
reduce
approach
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const data = [ { "level": "level3", "category": "car" }, { "level": "level1", "category": "bike" }, { "level": "level2", "category": "car" }, { "level": "level5", "category": "bike" } ];
const newObj = data.reduce((acc, item) => {
const { level, category } = item;
acc[category] = acc[category] || [];
acc[category].push(level);
return acc;
}, {});
console.log(newObj);
<!-- end snippet -->
The first approach is simple and understandable and the second is a more functional approach. But both approaches have a linear time complexity of O(n), choosing one of them is just a matter of personal preference and coding style.
答案2
得分: 1
使用Array.prototype.reduce
函数:
const array = [{
"level": "level3",
"category": "car"
}, {
"level": "level1",
"category": "bike"
}, {
"level": "level2",
"category": "car"
}, {
"level": "level5",
"category": "bike"
}];
const result = array.reduce((a, {level, category}) => {
(a[category] = a[category] || []).push(level);
return a;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
英文:
By using the function Array.prototype.reduce
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const array = [{ "level": "level3", "category": "car" }, { "level": "level1", "category": "bike" }, { "level": "level2", "category": "car" }, { "level": "level5", "category": "bike" }],
result = array.reduce((a, {level, category}) => {
(a[category] = a[category] || []).push(level);
return a;
}, {});
console.log(result);
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论