Kishan Sharma
Kishan Sharma

Reputation: 47

Array to array of objects

I have an array of country such as:

country=["Afganistan","Albania","Algeria"]

How can I convert this array into array of objects such as:

newarray=[
 { key: 'Afghanistan', value: 'Afghanistan', text: 'Afghanistan' },
  { key: 'Albania', value: 'Albania', text: 'Albania' },
  { key: 'Algeria', value: 'Algeria', text: 'Algeria' }
]

Upvotes: 0

Views: 18

Answers (2)

Kishan Sharma
Kishan Sharma

Reputation: 47

I found the answer. The method will be:-

let newarray = [];
    country.map(item => {
      return newarray.push({ key: item, value: item, text: item })
    })

Upvotes: 0

ThibautM
ThibautM

Reputation: 71

country=["Afganistan","Albania","Algeria"]

let newarray = [];
country.forEach(item => {
  newarray.push({
    key: item,
    value: item,
    text: item
  });
});

console.log(newarray);

Upvotes: 2

Related Questions