Javascript 将深度嵌套对象的结构修改为对象数组
我有一个深度嵌套的javascript对象,它可以包含一个properties字段,这是一个对象,其中键表示属性名称,值表示其余的属性数据。我想将属性对象转换为一个对象数组,其中键与值组合在一起。 例如,这就是我所拥有的:Javascript 将深度嵌套对象的结构修改为对象数组,javascript,arrays,object,recursion,Javascript,Arrays,Object,Recursion,我有一个深度嵌套的javascript对象,它可以包含一个properties字段,这是一个对象,其中键表示属性名称,值表示其余的属性数据。我想将属性对象转换为一个对象数组,其中键与值组合在一起。 例如,这就是我所拥有的: const test = { properties: { meta: { type: 'object', properties: { agencyId: { type: 'stri
const test = {
properties: {
meta: {
type: 'object',
properties: {
agencyId: {
type: 'string',
example: 'e767c439-08bf-48fa-a03c-ac4a09eeee8f',
description: 'agencyId',
},
},
},
data: {
type: 'object',
properties: {
intervalStartTime: {
type: 'string',
description: 'Time in GMT',
example: '1591702200000',
},
group: {
type: 'object',
properties: {
groupType: {
type: 'string',
description: 'Group Type',
example: 'vip',
},
numberofRequests: {
type: 'number',
example: 10198,
description: 'Amount of requests coming from group',
},
},
},
},
},
},
}
这就是我想要的:
const test = {
properties: [
{
name: 'meta',
type: 'object',
properties: [
[
{
type: 'string',
example: 'e767c439-08bf-48fa-a03c-ac4a09eeee8f',
description: 'agencyId',
name: 'agencyId',
},
],
],
},
{
name: 'data',
type: 'object',
properties: [
[
{
type: 'string',
description: 'Time in GMT',
example: '1591702200000',
name: 'intervalStartTime',
},
{
name: 'group',
type: 'object',
properties: [
{
name: 'groupType',
type: 'string',
description: 'Group Type',
example: 'vip',
},
{
name: 'numberOfRequests',
type: 'number',
example: 10198,
description: 'Amount of requests coming from group',
},
],
},
],
],
},
],
}
我有一个helper函数,可以将对象转换为我想要的形式,但是我正在努力递归地修改嵌套属性。这就是我所拥有的。关于如何将整个对象修改为所需的结构,有什么建议吗
const convertObj = (obj) => {
return Object.entries(obj).reduce((initialVal, [name, nestedProperty]) => {
initialVal.push({ ...nestedProperty, name })
return initialVal
}, [])
}
const getNestedProperties = (data) => {
for (const key in data) {
const keyDetails = data[key]
if (keyDetails.hasOwnProperty('properties')) {
const keyProperties = keyDetails['properties']
keyDetails['properties'] = []
keyDetails['properties'].push(convertObj(keyProperties))
getNestedProperties(keyProperties)
}
}
}
如果对象具有
属性
键,则映射条目并创建一个对象数组,每个键为名称
,其余为值。循环遍历对象并检查每个属性是否为对象。如果是,则递归调用该函数。{…o}
部分创建输入的一个副本,以便它不会发生变异
const test={properties:{meta:{type:“object”,properties:{agencyId:{type:“string”,示例:“e767c439-08bf-48fa-a03c-ac4a09eeee8f”,描述:“agencyId”,},},},},},数据:{type:“object”,属性:{intervalStartTime:{type:{type:“string”,描述:“格林威治时间”,示例:“1591702200000”,},组:{type:“object properties:{groupType:{type:{string”,description:“组类型”,示例:“vip”,},numberofRequests:{Type:“number”,示例:10198,description:“来自组的请求量”,};
函数转换({…o}){
用于(常量键输入o){
如果(o[键]=“对象”的类型)
o[key]=转换(o[key])
}
如果(o.hasOwnProperty(“属性”))
o、 properties=Object.entries(o.properties).map(([name,v])=>({name,…v}))
返回o
}
log(console(convert(test))
如果没有属性
属性,只需返回对象的其余部分。如果这样做,则返回对象的其余部分加上一个属性
数组属性,该数组属性是通过获取该属性中的每个名称值条目,并通过将属性名称
添加到对该值
调用转换
的结果中,将其转换为对象而形成的
代码相当简单:
const transform=({properties,…rest}={})=>
性质
? {
休息
属性:Object.entries(properties.map)([name,val])=>({
名称
…转换(val)
}))
}
:{……休息}
const test={properties:{meta:{type:'object',properties:{agencyId:{type:'string',示例:'e767c439-08bf-48fa-a03c-ac4a09eeee8f',说明:'agencyId'}}}},数据:{type:'object',属性:{intervalStartTime:{type:'string',说明:'GMT',示例:'1591702200000',oup:{type:'object properties:{grupType:{type:'string',description:'Group type',example:'vip'},numberofRequests:{type:'number',example:10198,description:'Amount of requests from Group'}
console.log(转换(测试))
.as控制台包装{max height:100%!important;top:0}
这里有一个解决方案,在这里进行重载。请注意,这是有效的,因为遍历是按删除安全顺序进行的
//const objectScan=require('object-scan');
const test={properties:{meta:{type:'object',properties:{agencyId:{type:'string',示例:'e767c439-08bf-48fa-a03c-ac4a09eeee8f',说明:'agencyId'}}},数据:{type:'object',属性:{intervalStartTime:{type:'string',说明:'GMT',示例:'1591702200000',组:{type:'object properties:{groupType:{type:'string',description:'Group type',example:'vip'},numberofRequests:{type:'number',example:10198,description:'Amount of requests from Group'};
常量重写=(obj)=>objectScan(['**.properties']{
rtn:'count',//返回重写次数
filterFn:({value,parent,property})=>{
父[属性]=对象。条目(值)
.map(([name,v])=>({name,…v}));
}
})(obj);
console.log(重写(测试));
// => 4
控制台日志(测试);
//=>{properties:[{name:'meta',type:'object',properties:[{name:'agencyId',type:'string',示例:'e767c439-08bf-48fa-a03c-ac4a09eeee8f',description:'agencyId'}},{name:'data',type:'object properties:[{name:'intervalStartTime',type:'string',description:'GMT',示例:'1591702200000'},{name:'group',type:'object',properties:[{name:'groupType',type:'string',description:'group type',example:'vip'},{name:'numberofRequests',type:'number',example:10198,description:'amountofrequests from group'}]}]}
。作为控制台包装{最大高度:100%!重要;顶部:0}