如何使用es6从现有对象创建被操纵的新对象

时间:2017-10-06 09:03:27

标签: javascript object ecmascript-6

我必须从obj2创建obj1obj1可以拥有无​​限制的嵌套图层。 如何用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"
        }
      }
    }
};

1 个答案:

答案 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; }

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#mapspread syntax ...

更聪明一些

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; }