如何使用 javascript 将嵌套的 json 数据转换为逗号分隔的列表

How to convert nested json data into a comma separated list with javascript

提问人:Helpme 提问时间:1/4/2022 最后编辑:mkrieger1Helpme 更新时间:2/10/2022 访问量:494

问:

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函数分隔键和值。但是由于所有产品数据都采用相同的格式,因此我对地图功能在这里的工作方式感到困惑。

JavaScript jQuery 数组 JSON 嵌套

评论

0赞 freedomn-m 1/4/2022
你试过map()吗?data.map(e => `${e.name},${e.id},${e.price}`).join("\n")
0赞 Helpme 1/4/2022
我改变了问题。请检查一下并提及一些想法。

答:

0赞 Zlatov 1/4/2022 #1

您要求的数据格式对分析不友好!

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赞 Helpme 1/4/2022
对不起,我已经更新了问题。因此,请检查一下,如果您对如何解决它有任何想法,请提及
0赞 Zlatov 1/4/2022
在这一点上,我仍然不明白问题本身。也许我的英语翻译对我没有多大帮助。我可以猜,也许您希望能够通过其 ID 访问产品,例如:data[“1”] 或 data[“3”] ?
0赞 Zlatov 1/4/2022
javascript 有数组和哈希值,遍历数组和哈希值略有不同: [1,2,3].forEach(function(element, index, array) { element ... }) 和 hash: for(var key in data) { data[key] ... })
0赞 Helpme 1/4/2022
哦,对不起.....请立即查看
0赞 Zlatov 1/4/2022
Product1: "productname", Product1Price: 0.00---它不是数组,也不是哈希=)!哈希值: 数组:{"key_as_string": "Value"}["String", 100]
1赞 freedomn-m 1/4/2022 #2

您仍然可以使用此答案中的详细信息来迭代每个属性:.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),那么您必须对输出进行硬编码,就像在另一个答案中一样。

评论

0赞 Helpme 1/4/2022
嘿,谢谢你的回答。但是你能把它做成 product1price , product1id , product1name , product2name , product2id , product2price 吗?就像没有钥匙一样。
0赞 freedomn-m 1/4/2022
输出与您提供的输出示例匹配。不清楚你说的“就像没有钥匙一样”是什么意思。
0赞 Helpme 1/4/2022
感谢您宝贵的回复 @freedomn-m。我已经更新了问题。现在,您可以检查ID是否已更改。现在我想要的是输出数据的格式应该是 product1name: “productname” , product1id: “1356” , product1price: “0.00”.......product2name: “productname2” , product2id: “5263” , product2price: “0.00”........像这样的东西
0赞 freedomn-m 1/4/2022
好的,所以你想要一个索引而不是数据的 id - 更新。