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 函式將鍵和值分開。但由于所有產品資料的格式相同,所以我對地圖功能在這里的作業方式感到困惑。
先謝謝了。
uj5u.com熱心網友回復:
您仍然可以使用此答案中的.map詳細資訊來迭代每個屬性:
var data = [{
name: "productname",
id: "1234",
price: "0.00",
category: "Health",
position: "1",
list: "New Products",
stocklevel: "20",
brand: "Health"
},
{
name: "productname2",
id: "2345",
price: "0",
category: "Hair",
position: "2",
list: "New Products",
stocklevel: "",
brand: "Hair"
},
{
name: "productname3",
id: "356anything",
price: "0.00",
category: "skin",
position: "3",
list: "New Products",
stocklevel: "10",
brand: "skin"
},
]
console.log(
data.map((e, i) => {
return Object.keys(e).map(p => `Product${i 1}${p=="name"?"":p}:"${e[p]}"`).join(",\n")
}).join(",\n")
);
添加\n以匹配預期輸出,不清楚您是希望它們在單行(逗號分隔串列)還是與示例中的不同行。
如果您不希望引號中的數字值,那么您將首先檢查它們是否是數字等,因為您的原始值將它們放在引號中。
或者,如果您希望它們按特定順序/不同的情況(您的一個示例具有 Product1Price 而不是 Product1price),那么您將必須對輸出進行硬編碼,如另一個答案。
uj5u.com熱心網友回復:
你問的資料格式不是決議友好的!
var data = [
{
name: "productname",
id: "1",
price: "0.00",
category: "Health",
position: "1",
list: "New Products",
stocklevel: "20",
brand: "Health"
},
{
name: "productname2",
id: "2",
price: "0",
category: "Hair",
position: "2",
list: "New Products",
stocklevel: "",
brand: "Hair"
},
{
name: "productname3",
id: "3",
price: "0.00",
category: "skin",
position: "3",
list: "New Products",
stocklevel: "10",
brand: "skin"
},
]
var result = {}
data.forEach(function(e) {
var key = "Product" e.id
result[key] = e.name
result[key "Price"] = e.price
result[key "Category"] = e.category
result[key "Position"] = e.position
result[key "List"] = e.list
result[key "Stocklevel"] = e.stocklevel
result[key "Brand"] = e.brand
})
console.log('result: ', result)
var data = {
1356: {
name: "productname",
id: "1356",
price: "0.00",
category: "Health",
position: "1",
list: "New Products",
stocklevel: "20",
brand: "Health"
},
5263: {
name: "productname2",
id: "5263",
price: "0",
category: "Hair",
position: "2",
list: "New Products",
stocklevel: "",
brand: "Hair"
},
7473: {
name: "productname3",
id: "7473",
price: "0.00",
category: "skin",
position: "3",
list: "New Products",
stocklevel: "10",
brand: "skin"
}
}
// Access to any product in the data set
console.log('product 5263: ', data[5263])
// And also access to any product property directly from the dataset
console.log('product name 5263: ', data[5263].name)
// Looping through the array of goods and selecting the required fields
Object.keys(data).map(function(key) {
var product = data[key]
// Output only name and price:
console.log(product.name, ' ', product.price)
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/403337.html
標籤:
