2

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
  • you could consider adding the full code if this is something you are just trying out. if the object is coming from the json, the json details would be useful too. Commented Sep 19, 2020 at 8:04

1 Answer 1

2

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}`);
Sign up to request clarification or add additional context in comments.

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.