How to extract the objects from an array to put them in variables automatically?

Viewed 75

How to extract the objects from an array to put them in variables that are created according to the index of the array that corresponds to the object

I tried it :

response.forEach((el, i, )=>{

let nameProduct[i] = ` the name of product  : ${response[i].nameProduct}`;
console.log(nameProduct[i]);
});

but it's a fail

1 Answers

You could map the values to a global (or at least outside of map) variable.

const nameProduct = response.map(({ nameProduct }) => `the name of product: ${nameProduct}`);
Related