如何从数组中提取对象以将其自动放入变量中?

时间:2020-09-19 07:40:40

标签: javascript arrays object variables

如何从数组中提取对象以将其放入根据对象对应的数组索引创建的变量中

我尝试过:

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

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

但这是失败的

1 个答案:

答案 0 :(得分:2)

您可以将值映射到全局变量(或至少在map之外)。

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