1

I have an array of strings.

const periods = ["2021-03-31", "2021-02-28", "2021-01-31"]

I want to create a dictionary array from this array. My result should look like this:

[
   {value: "2021-03-31",label : "2021-03-31"}, 
   {value: "2021-02-28",label : "2021-02-28"}, 
   {value: "2021-01-31",label : "2021-01-31"}
]

Is it possible to do it with map function.

const periodsDictArray = periods.map(el => ??? )

1 Answer 1

2

You have to return an object from array#map callback.

const periods = ["2021-03-31", "2021-02-28", "2021-01-31"],
      result = periods.map(value => ({value, label: value}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

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.