【发布时间】:2022-01-04 11:14:02
【问题描述】:
var data= [
{
name: "productname",
id: "1356",
price: "0.00",
category: "Health",
position: "1",
list: "New Products",
stocklevel: "20",
brand: "Health"
},
{
name: "productname2",
id: "5263",
price: "0",
category: "Hair",
position: "2",
list: "New Products",
stocklevel: "",
brand: "Hair"
},
{
name: "productname3",
id: "7473",
price: "0.00",
category: "skin",
position: "3",
list: "New Products",
stocklevel: "10",
brand: "skin"
},
]
我有多个产品的数据,按照嵌套顺序,产品最多可以达到 n 个。
但我希望将这些数据转换为逗号分隔值作为列表,这样我就可以让它对任何内容都友好。
这样
Product1: "productname",
Product1Price: 0.00,
Product1id: "1356",
Product1brand: "health",
Product1stocklevel: "20",
Product2: "productname2",
Product2price: 0,
Product2id: "5263",
Product2brand: "hair",
Product2stocklevel: "",
Product3: "productname3",
Product3price: 0.00,
Product3id: "7473",
产品详细信息应在下一个循环之外显示
我们可以用 map 函数分离键和值。但是由于所有产品数据的格式都相同,所以我对地图功能在这里的工作方式感到困惑。
【问题讨论】:
-
你试过map()吗?
data.map(e => `${e.name},${e.id},${e.price}`).join("\n") -
我已经改变了问题。请检查一下并提出一些想法。
标签: javascript jquery arrays json nested