2

In api return value like this,

[
    "Action",
    "Comedy",
    "Documentary",
    "Drama"
]

I need to modify the above array like this,

 const techCompanies = [
      { label: "Action", value: 1 },
      { label: "Comedy", value: 2 },
      { label: "Documentary", value: 3 },
      { label: "Drama", value: 4 }
   ];

I used Axios to get data,

const response = await axios.get("http://localhost:3000/Movie/v1.0/genre");
2
  • 3
    how you are getting property value ? Commented May 5, 2019 at 14:16
  • may be array index Commented May 5, 2019 at 14:34

1 Answer 1

4

This is an array so you can you use the .map() function to do that:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

const arr = ["Action", "Comedy", "Documentary", "Drama"];

arr.map((arrElement, index) => ({ label: arrElement, value: index }));
Sign up to request clarification or add additional context in comments.

9 Comments

cannot use vale: index
because the first element is 0 or for some other reason?
no, when using value: error message coming ';' expected.
I typed in your link also, this error is coming developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
var Data = [1, 4, 9, 16]; // pass a function to map const map1 = Data.map((arrElement, index) => { label: arrElement, value: index }); console.log(map1);
|

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.