简体   繁体   中英

Javascript transform array of object structure

I am trying to transform an array of objects structure from looking like this:

[
  {
    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"
      }
    ]
  }
]

Into this:

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

So more or less I want to take two values from entities and make them into key/values in the root object instead.

I have tried using Object.entries(data).map() but I am stuck

You could use a destruction for collecting all wanted properties and build a new object.

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

Use Array.map()

  1. Create two new key value pairs entitiesName and entitiesType
  2. Delete the entities key

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

To avoid modification on source array.

 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 } 

You can do that by using Array.map function of array and spread operator

 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM