我必须从obj1
创建obj2
。obj1
可以具有无限的嵌套层。如何使用ES6函数创建?
我尝试了Object.keys(obj).forEach
等。我不确定我明白。我做错了什么
我有这个对象(属性密钥有太多的子键,例如电话,地址等)
)obj1 = {
type: "object",
properties:{
Person:{
title:"",
type:"object",
properties:{
Name:{
type:"string",
uiType:"input"
},
Surname:{
type:"string",
uiType:"input"
},
}
},
General:{
title:"",
type:"object",
properties:{
height:{
type:"string",
uiType:"number"
}
Nested:{
title:'',
type:'object',
properties:{
Nested1:{
type:"string",
uiType:"textarea"
},
Nested2:{
type:"string",
uiType:"textarea"
}
}
}
}
}
}
};
我需要这个
const obj2 = {
Person:{
Name:{
type: "input"
},
Surname:{
type: "input"
}
},
General:{
height:{
type: "number"
}
Nested: {
Nested1: {
type: "textarea"
},
Nested2: {
type: "textarea"
}
}
}
};
检查对象时可以采用迭代和递归方法。
function build(source, target) {
if (source.type === 'object') {
Object.keys(source.properties).forEach(function (k) {
build(source.properties[k], target[k] = {});
});
} else {
target.type = source.uiType;
}
return target;
}
var object = { type: "object", properties: { Person: { title: "", type: "object", properties: { Name: { type: "string", uiType: "input" }, Surname: { type: "string", uiType: "input" }, } }, General: { title: "", type: "object", properties: { height: { type: "string", uiType: "number" }, Nested: { title: '', type: 'object', properties: { Nested1: { type: "string", uiType: "textarea" }, Nested2: { type: "string", uiType: "textarea" } } } } } } },
result = build(object, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
使用Array#reduce
function build(source) {
return source.type === 'object'
? Object.keys(source.properties).reduce((r, k) =>
Object.assign(r, { [k]: build(source.properties[k]) }),
{})
: { type: source.uiType };
}
var object = { type: "object", properties: { Person: { title: "", type: "object", properties: { Name: { type: "string", uiType: "input" }, Surname: { type: "string", uiType: "input" }, } }, General: { title: "", type: "object", properties: { height: { type: "string", uiType: "number" }, Nested: { title: '', type: 'object', properties: { Nested1: { type: "string", uiType: "textarea" }, Nested2: { type: "string", uiType: "textarea" } } } } } } };
console.log(build(object));
.as-console-wrapper { max-height: 100% !important; top: 0; }
使用Array#map
和传播语法...
function build(source) {
return source.type === 'object'
? Object.assign(
...Object.keys(source.properties).map(
k => ({ [k]: build(source.properties[k]) })
)
)
: { type: source.uiType };
}
var object = { type: "object", properties: { Person: { title: "", type: "object", properties: { Name: { type: "string", uiType: "input" }, Surname: { type: "string", uiType: "input" }, } }, General: { title: "", type: "object", properties: { height: { type: "string", uiType: "number" }, Nested: { title: '', type: 'object', properties: { Nested1: { type: "string", uiType: "textarea" }, Nested2: { type: "string", uiType: "textarea" } } } } } } };
console.log(build(object));
.as-console-wrapper { max-height: 100% !important; top: 0; }