【问题标题】:How to convert nested json data into a comma separated list with javascript如何使用javascript将嵌套的json数据转换为逗号分隔的列表
【发布时间】: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


【解决方案1】:

您仍然可以使用 .map 以及来自 this answer 的详细信息来迭代每个属性:

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),那么您必须对输出进行硬编码,就像在另一个答案中一样。

【讨论】:

  • 嘿,谢谢你的回答。但是你能不能把它设为 product1price , product1id , product1name , product2name , product2id , product2price 。就像没有钥匙一样。
  • 输出与您提供的输出样本相匹配。不清楚“就像没有钥匙”是什么意思。
  • 感谢您的宝贵回复@freedomn-m。我已经更新了这个问题。现在您可以检查 id 是否已更改。现在我想要的是输出数据的格式应该像 product1name: "productname" , product1id: "1356" , product1price: "0.00"....... product2name: "productname2" , product2id: "5263" , product2price: “0.00”............像这样的东西
  • 好的,所以你想要一个索引而不是数据的 id - 已更新。
【解决方案2】:

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

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)
})

【讨论】:

  • 对不起,我已经更新了问题。所以请检查一下,如果您对我该如何解决它有任何想法,请提及
  • 至此,我还是不明白问题本身。也许我的英文翻译对我帮助不大。我可以猜到,也许您希望能够通过其 ID 访问产品,例如:data["1"] 或 data["3"]?
  • javascript 有数组和哈希,遍历数组和哈希略有不同:[1,2,3].forEach(function(element, index, array) { element ... }) 和哈希: for(var key in data) { data[key] ... })
  • 对不起!......请立即查看
  • Product1: "productname", Product1Price: 0.00 --- 不是数组,也不是哈希 =) !哈希:{"key_as_string": "Value"} 数组:["String", 100]
猜你喜欢
  • 1970-01-01
  • 2020-12-16
  • 2018-04-24
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多