0
  allInfo.map((name) => console.log("arr", name.firstName))

The above statement is an array. I want to iterate the values and put them in the following array to show it in the dropdown.

    const info = [
        { value: "firstName", label: "firstName" },
        { value: "first",label: "first"},
        { value: "lastName", label: "lastName" }
    ]

I want to iterate the value of allInfo and put it inside the value of info. Instead of value = "firstName", I want to get it from the allInfo array. Can anyone help me with this?

0

1 Answer 1

3

Just map the allInfo array directly to get the info array.

const info = allInfo.map(name => ({
    value: name.firstName,
    label: name.firstName,
}));

If you want to have some static values in the info array too, you can use the spread operator like this:

const allInfoItems = allInfo.map(name => ({
    value: name.firstName,
    label: name.firstName,
}));

const info = [
    { value: 'something', label: 'something' },
    ...allInfoItems
]
Sign up to request clarification or add additional context in comments.

3 Comments

Hey Thanks, but I also want to put some static values inside this info, since I'll fetching this data in a dropdown. Some values I am getting from the backend while some are static. Any idea how to do that?
is it possible to have the const info containing static value also and values iterated from allInfo also? Because some values I am getting from the backend, while some are static in UI
Use the spread operator then - I've added it to the answer.

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.