拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何使用javascript将嵌套的json资料转换为逗号分隔的串列

如何使用javascript将嵌套的json资料转换为逗号分隔的串列

白鹭 - 2022-01-26 2076 0 0
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)
})

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *