【问题标题】:How to extract the objects from an array to put them in variables automatically?如何从数组中提取对象以自动将它们放入变量中?
【发布时间】:2021-01-06 00:34:17
【问题描述】:

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

我试过了:

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

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

但是失败了

【问题讨论】:

  • 如果您只是在尝试,可以考虑添加完整代码。如果对象来自 json,则 json 详细信息也会很有用。

标签: javascript arrays object variables


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2011-11-13
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多