如何从数组中提取对象以将其放入根据对象对应的数组索引创建的变量中
我尝试过:
response.forEach((el, i, )=>{
let nameProduct[i] = ` the name of product : ${response[i].nameProduct}`;
console.log(nameProduct[i]);
});
但这是失败的
答案 0 :(得分:2)
您可以将值映射到全局变量(或至少在map
之外)。
const nameProduct = response.map(({ nameProduct }) => `the name of product: ${nameProduct}`);