-2

I want to turn something like this:

a = ['London', 'Brighton', 'Newcastle']

into something like this:

b = [{CityName: 'London'},{CityName: 'Brighton'},{CityName: 'Newcastle'}]

Any help would be much appreciated!

2
  • 4
    a.map(CityName => ({CityName})) Commented Jan 4, 2022 at 19:31
  • 1
    @HassanImam - you should capitalize CityName and write as an answer so I can upvote you :-) Commented Jan 4, 2022 at 19:33

1 Answer 1

0

You can use array#map to create an array of object.

a.map(CityName => ({CityName}))

const arr = ['London', 'Brighton', 'Newcastle'],
      result = arr.map(CityName => ({CityName}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.