-6
const myArray =  ['position zero', 'position one', 'position three', 'position four'];

// i neeed to convert it to

const objectArray = [
    {
        position: 'position zero'
    },
    {
        position: 'position one'
    },
    {
        position: 'position three'
    },
    {
        position: 'position four'
    },
];

// must the same key which i will be refer to all

2

4 Answers 4

3

map over your current array and return the object from map function

const myArray =  ['position zero', 'position one', 'position three', 'position four'];

const res = myArray.map(data => {
    return {position: data}
})

console.log(res)

1
  • Consider marking the answer as accepted, if it helped Commented Apr 4, 2018 at 10:46
0

You can use array#map with Shorthand property names.

const myArray =  ['position zero', 'position one', 'position three', 'position four'],
      result = myArray.map(position => ({position}));
console.log(result);
.as-console-wrapper { max-height:100% !important; top:0;}

0
0

You can use .map() here:

const result = myArray.map(s => ({position: s}));

Demo:

const myArray =  ['position zero', 'position one', 'position three', 'position four'];

const result = myArray.map(s => ({position: s}));

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

Docs:

0

User array.map with ES6's shorthand object litteral:

const myArray =  ['position zero', 'position one', 'position three', 'position four'];

const res = myArray.map(position => ({position}));
console.log(res);

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.