1

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

3
  • Indentation helps Commented Feb 13, 2018 at 15:10
  • What did you try exactly? Commented Feb 13, 2018 at 15:13
  • There are so may questions about this on the site about doing this. Can you please try more things, do some research? Also, be clear on the logic to be used to get from one to another. Commented Feb 13, 2018 at 15:13

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

You're modifying the source array, so you don't need to return a "new" array.
0

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
}

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.