[英]How to convert array of nested objects to objects in javascript?
I have got array of nested array of objects .我有一组嵌套的对象数组。 I want to get only Permission objects from data and I need to convert data array to permission objects .我只想从数据中获取权限对象,我需要将数据数组转换为权限对象。
"data":[
{
"Book":[
{
"label":"Can View",
"value":"can_view"
},
{
"label":"Can Create",
"value":"can_create"
},
]
},
{
"Articles":[
{
"label":"Can View",
"value":"can_view"
},
]
},
{
"Journals":[
{
"label":"Can Upload",
"value":"can_upload"
},
{
"label":"Can Download",
"value":"can_download"
}
]
},
{
"Permission":[
{
"label":"Can View",
"value":"can_view"
},
{
"label":"Can Create",
"value":"can_create"
}
]
}
]
I am trying to get permission objects from list of my data objects.我正在尝试从我的数据对象列表中获取权限对象。 I am using filter method for geting Permission array and want to convert permission objects, but I got nested arrays .我正在使用 filter 方法获取 Permission 数组并想转换权限对象,但我得到了嵌套数组。 where is the problem?问题出在哪儿?
let dataPer = data.permissions.filter(item=>item.Permission).map(item=>item.Permission)
console.log(dataPer)
My accepted output would be :我接受的输出是:
let output= {
Permission:{
can_view:"can_view",
can_create:"can_create"
}
}
Well you could try something like this :那么你可以尝试这样的事情:
let data=[ { "Book":[ { "label":"Can View", "value":"can_view" }, { "label":"Can Create", "value":"can_create" } ] }, { "Articles":[ { "label":"Can View", "value":"can_view" } ] }, { "Journals":[ { "label":"Can Upload", "value":"can_upload" }, { "label":"Can Download", "value":"can_download" } ] }, { "Permission":[ { "label":"Can View", "value":"can_view" }, { "label":"Can Create", "value":"can_create" } ] } ]; let myPermissionObjects = []; let permissionConvert = ()=>{ for(let current in data) for(let currentObject in data[current]) for(let currentResult in data[current][currentObject]) myPermissionObjects.push(data[current][currentObject][currentResult]); }; permissionConvert(); console.log(myPermissionObjects);
if i understood your problem correctly如果我正确理解你的问题
for the object permission only :仅用于对象权限:
let data=[ { "Book":[ { "label":"Can View", "value":"can_view" }, { "label":"Can Create", "value":"can_create" } ] }, { "Articles":[ { "label":"Can View", "value":"can_view" } ] }, { "Journals":[ { "label":"Can Upload", "value":"can_upload" }, { "label":"Can Download", "value":"can_download" } ] }, { "Permission":[ { "label":"Can View", "value":"can_view" }, { "label":"Can Create", "value":"can_create" } ] } ]; let myPermissionObjects = []; let permissionConvert = ()=>{ for(let current in data) for(let currentObject in data[current].Permission) myPermissionObjects.push( data[current].Permission[currentObject]); }; permissionConvert(); console.log(myPermissionObjects); let onePermission = []; for(let next in myPermissionObjects) { onePermission["Permission"]=myPermissionObjects[next]; console.log(myPermissionObjects[next]); }
You can do it with reduce
function, like this:您可以使用reduce
函数来完成,如下所示:
const data = [
{
Book: [
{
label: "Can View",
value: "can_view"
},
{
label: "Can Create",
value: "can_create"
}
]
},
{
Articles: [
{
label: "Can View",
value: "can_view"
}
]
},
{
Journals: [
{
label: "Can Upload",
value: "can_upload"
},
{
label: "Can Download",
value: "can_download"
}
]
},
{
Permission: [
{
label: "Can View",
value: "can_view"
},
{
label: "Can Create",
value: "can_create"
}
]
}
];
-- ——
function getPermissionList() {
const list = data
.filter(item => item.Permission)[0]
.Permission.reduce((list, item) => {
list.push({ [item.label]: item.value });
return list;
}, {});
return Object.assign({}, list);
}
I got the solution , but is it best for my code我得到了解决方案,但它是否最适合我的代码
let dataPer = data.permissions.filter(item=>item.Permission)
const result = Object.assign({},dataPer)
let res = result[0].Permission.map(item=>{
return {
[item.value]:item.value
}
})
const obj = Object.assign({},res)
console.log(obj[0].can_view)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.