Home > Software engineering >  How to convert nested json data into a comma separated list with javascript
How to convert nested json data into a comma separated list with javascript

Time:01-05

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"
      },
      
      
     ]

i have data of multiple product as in nested order the product could be upto n.

but i want this data into commma seprated values As list so i can make it anylaytical friendly.

Like this

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",

The product details should be show out of the nexted loop

We can separate key and values with map function. But as all product data is in same format so i am confused about how map function would work here.

Thanks in advanced.

CodePudding user response:

You can still use .map along with details from this answer to iterate each property:

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")
);

Added \n to match the expected output, unclear if you want them on a single line (comma separated list) or different lines as in your sample.

If you don't want the number values in quotes, then you'll have check if they're numbers first etc as your original has them in quotes.

Alternatively, and if you want them in a specific order / different case (one of your examples has Product1Price instead of Product1price) then you'll have to hardcode the output, as in the other answer.

CodePudding user response:

The data format you are asking is not analytic friendly!

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)

  •  Tags:  
  • Related