[Vuejs]-Is there a quicker way to copy a value in a key of data object to a new key in the same data object in javascript?

2👍

What you’re doing will work but there are certainly some unnecessary steps as written. There’s no need to copy the API response data into an array and then change the array contents when you can map the API response itself.

// You can do steps 1 2 and 3 all in 1 .map()
const stringOptionsOutlet = response.data.outlet.map(v => {
    return {
        ...v,
        value: v.id
    }
})

The downside of doing it all in 1 way is that if you want to do some more complex logic that single .map call could get very cluttered and maybe it’d be simpler to introduce a separate step, with the caveat that you’d then process the data a second time.

1👍

Try this:

stringOptionsOutlet.map((v) => ({ ...v, value: v.id }));

Leave a comment