英文:
How to loop through an array and get output in object
问题
我正在从我的数据库获取数据,并希望按需循环。
我从我的数据库获取到对象数组,我希望将其转换为带有一些更改的对象。
我的数据:
Data= [
{
"Menu": "Menu1",
"SubMenu": "dash",
"PATH": "Path1"
},
{
"Menu": "Menu2",
"SubMenu": "SubMenu1",
"PATH": "path2"
},
{
"menu": "menu2",
"SubMenu": "SubMenu2",
"PATH": "path3"
},
{
"Menu": "menu2",
"Submenu": "SubMenu3",
"PATH": "Path4"
}
]
我想要将其转换成以下格式:
{
"menu1": [
{
"SubMenu": "dash",
"PATH": "path1"
}
],
"menu2": [
{
"SubMenu": "subMenu1",
"PATH": "Path2"
},
{
"SubMenu": "Submenu2",
"PATH": "Path3"
},
{
"type": "SubMenu3",
"link": "Path4"
}
]
}
const newArray= Data.map(element => element.Menu);
console.log(newArray);
希望这些信息对你有所帮助。
英文:
I am getting Data from my db And I want to loop as required
I am getting array of objects from my data base and I want to convert it to objects with some changes
My Data
Data= [
{
"Menu": "Menu1",
"SubMenu": "dash",
"PATH": "Path1"
},
{
"Menu": "Menu2",
"SubMenu": "SubMenu1",
"PATH": "path2"
},
{
"menu": "menu2",
"SubMenu": "SubMenu2",
"PATH": "path3"
},
{
"Menu": "menu2",
"Submenu": "SubMenu3",
"PATH": "Path4"
}
]
I want to convert it into like below -:
{
"menu1": [
{
"SubMenu": "dash",
"PATH": "path1"
}
],
"menu2": [
{
"SubMenu": "subMenu1",
"PATH": "Path2"
},
{
"SubMenu": "Submenu2",
"PATH": "Path3"
},
{
"type": "SubMenu3",
"link": "Path4"
}
]
}
const newArray= Data.map(element => element.Menu);
console.log(newArray);
答案1
得分: 2
你可以使用 Array.reduce()
:
var Data = [{
"Menu": "Menu1",
"SubMenu": "dash",
"PATH": "Path1"
},
{
"Menu": "Menu2",
"SubMenu": "SubMenu1",
"PATH": "path2"
},
{
"Menu": "Menu2",
"SubMenu": "SubMenu2",
"PATH": "path3"
},
{
"Menu": "Menu2",
"SubMenu": "SubMenu3",
"PATH": "Path4"
}
];
var res = Data.reduce((acc, item) => {
var accObj = {
Submenu: item.SubMenu,
PATH: item.PATH
};
if (acc[item.Menu]) {
acc[item.Menu].push(accObj);
} else {
acc[item.Menu] = [accObj];
}
return acc;
}, {});
console.log(res);
英文:
You can use Array.reduce()
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var Data = [{
"Menu": "Menu1",
"SubMenu": "dash",
"PATH": "Path1"
},
{
"Menu": "Menu2",
"SubMenu": "SubMenu1",
"PATH": "path2"
},
{
"Menu": "Menu2",
"SubMenu": "SubMenu2",
"PATH": "path3"
},
{
"Menu": "Menu2",
"SubMenu": "SubMenu3",
"PATH": "Path4"
}
];
var res = Data.reduce((acc, item) => {
var accObj = {
Submenu: item.SubMenu,
PATH: item.PATH
};
if(acc[item.Menu]) {
acc[item.Menu].push(accObj);
} else {
acc[item.Menu] = [accObj];
}
return acc;
}, {});
console.log(res);
<!-- end snippet -->
答案2
得分: 0
你使用 map
是正确的方向,但在迭代之前应该初始化一个对象(result = {}
),然后随着迭代的进行逐步填充它。然后,你将不再使用 .map
,而应该使用 .forEach
,因为你不会使用输出。
const result = {}
Data.forEach(item => result[item.Menu] = {
//...
})
英文:
You're in the right direction with map
, however you should initialise an object before you iterate (result = {}
) and populate it as you go. You'd then not use .map
but .forEach
because you won't be using the output.
const result = {}
Data.forEach(item => result[item.Menu] = {
//...
})
</details>
# 答案3
**得分**: 0
输出结果:
```json
{
Menu1: [{
SubMenu: 'dash',
PATH: 'Path1'
}],
Menu2: [{
SubMenu: 'SubMenu1',
PATH: 'path2'
},
{
SubMenu: 'SubMenu2',
PATH: 'path3'
},
{
Submenu: 'SubMenu3',
PATH: 'Path4'
}
]
}
英文:
Try for this:
var obj = {};
Data.forEach(function(element){
var menu = element["Menu"];
delete element["Menu"];
if(Object.keys(obj).includes(menu)){
obj[menu].push(element)
} else {
obj[menu] = [element]
}
})
console.log("oo", obj);
OUTPUT:
{
Menu1: [{
SubMenu: 'dash',
PATH: 'Path1'
}],
Menu2: [{
SubMenu: 'SubMenu1',
PATH: 'path2'
},
{
SubMenu: 'SubMenu2',
PATH: 'path3'
},
{
Submenu: 'SubMenu3',
PATH: 'Path4'
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论