我必须从obj2
创建obj1
。 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"
}
}
}
};
答案 0 :(得分:2)
您可以在检查对象时采用迭代和递归方法。
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; }
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; }
更聪明一些
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; }