Javascript转换对象结构数组

时间:2018-02-13 15:09:56

标签: javascript object array-map

我正在尝试将对象结构数组转换为如下所示:

[
  {
    id: 1,
    val: "bool",
    name: "somename",
    entities: [
      {
        id: 1,
        name: "varchar",
        type: "string"
      }
    ]
  },
  {
    id: 2,
    val: "bool",
    name: "somename",
    entities: [
      {
        id: 1,
        name: "varchar",
        type: "string"
      }
    ]
  }
]

进入这个:

[
  {
    id: 1,
    val: "bool",
    name: "somename",
    entitiesName: "varchar",
    entitiesType: "string"
  },
  {
    id: 2,
    val: "bool",
    name: "somename",
    entitiesName: "varchar",
    entitiesType: "string"
  }
]

所以或多或少我想从entities获取两个值,并将它们变成根对象中的键/值。

我尝试过使用Object.entries(data).map(),但我遇到了问题

4 个答案:

答案 0 :(得分:3)

您可以使用销毁来收集所有想要的属性并构建新对象。



var array = [{ id: 1, val: "bool", name: "somename", entities: [{ id: 1, name: "varchar", type: "string" }] }, { id: 2, val: "bool", name: "somename", entities: [{ id: 1, name: "varchar", type: "string" }] }],
    result = array.map(
        ({ id, val, name, entities: [{ name: entitiesName, type: entitiesType }] }) =>
        ({ id, val, name, entitiesName, entitiesType })
    );

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:2)

使用Array.map()

  1. 创建两个新的键值对entitiesNameentitiesType
  2. 删除entities密钥
  3. var data = [{
        id: 1,
        val: "bool",
        name: "somename",
        entities: [{
          id: 1,
          name: "varchar",
          type: "string"
        }]
      },
      {
        id: 2,
        val: "bool",
        name: "somename",
        entities: [{
          id: 1,
          name: "varchar",
          type: "string"
        }]
      }
    ];
    
    data = data.map(
      (el) => {
          el.entitiesName = el.entities[0].name;
          el.entitiesType = el.entities[0].type;
          delete el.entities;
          return el;
      }
    );
    
    console.log(data);

答案 2 :(得分:0)

避免修改源数组。

var data = [  {    id: 1,    val: "bool",    name: "somename",    entities: [      {        id: 1,        name: "varchar",        type: "string"      }    ]  },  {    id: 2,    val: "bool",    name: "somename",    entities: [      {        id: 1,        name: "varchar",        type: "string"      }    ]  }];

var result = data.map((o) => {
  var obj = {...o, 'entitiesName': o.entities[0].name, 'entitiesType': o.entities[0].type };
  delete obj.entities;
  
  return obj;
});

console.log(data);
console.log(result);
.as-console-wrapper {
  max-height: 100% !important
}

答案 3 :(得分:0)

您可以通过使用数组的Array.map函数和扩展运算符

来实现

var data = [{
    id: 1,
    val: "bool",
    name: "somename",
    entities: [{
      id: 1,
      name: "varchar1",
      type: "string"
    }]
  },
  {
    id: 2,
    val: "bool",
    name: "somename",
    entities: [{
      id: 1,
      name: "varchar2",
      type: "string"
    }]
  }
];
var result = data.map(
  (el) => {
      const { entities, ...noentities } = el;
      return {...noentities, entitiesName :el.entities[0].name ,entitiesType:el.entities[0].type}
  }
);
console.log(result)