0

I have an Array with some Elements:

ArrayE = ["Car", "Bike", "Bus", "Truck"]

I have to make an array like this:

ArrayX = [
    { value: "Car", label: "Car" },
    { value: "Bike", label: "Bike" },
    { value: "Bus", label: "Bus" },
    { value: "Truck", label: "Truck" }
];

How can I convert the first Array to the second one?

2 Answers 2

2

You can use javascript map method to achieve this.

let ArrayE = ["Car","Bike","Bus","Truck"]

let ArrayX = ArrayE.map( el => ({'value':el,'label':el}))

console.log(ArrayX)

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

Comments

2

you can use javascript forEach method like below...

function myFuc(arr) {
    const result = [];
    arr.forEach(el => {
        result.push({
            value: el,
            label: el
        })
    }); 
    return result;
}

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.