-3
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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Helpme
  • 15
  • 4
  • Did you try [map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)? `data.map(e => \`${e.name},${e.id},${e.price}\`).join("\n")` – freedomn-m Jan 04 '22 at 11:24
  • I have changed the question. Please check it out and mention some idea. – Helpme Jan 04 '22 at 12:14

2 Answers2

1

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.

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • hey thanks for answer. But can you please make it as product1price , product1id , product1name , product2name , product2id , product2price. As like without key. – Helpme Jan 04 '22 at 13:41
  • Output matches your provided output sample. Unclear what you mean by "*as like without key*". – freedomn-m Jan 04 '22 at 13:54
  • Thanks for your valueble reply @freedomn-m. I have updated the question. Now you can check that the id has been changed. Now what i want is output data should be in format like product1name: "productname" , product1id: "1356" , product1price: "0.00"....... product2name: "productname2" , product2id: "5263" , product2price: "0.00"........ Something like this – Helpme Jan 04 '22 at 14:02
  • Ok, so you want an index rather than the data's id - updated. – freedomn-m Jan 04 '22 at 14:05
0

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)

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)
})
Zlatov
  • 126
  • 1
  • 6
  • Sorry , i have updated the question. So please check it out and if you have any idea about how can i solve it pls mention – Helpme Jan 04 '22 at 12:13
  • At this point, I still don't understand the problem itself. Perhaps my translator from English doesn't help me much. I can guess, maybe you want to be able to access products by their ID like: data["1"] or data["3"] ? – Zlatov Jan 04 '22 at 12:33
  • javascript has arrays and hashes, iterating over arrays and hashes is slightly different: [1,2,3].forEach(function(element, index, array) { element ... }) and hash: for(var key in data) { data[key] ... }) – Zlatov Jan 04 '22 at 12:40
  • Oo sorry !.....check now please – Helpme Jan 04 '22 at 12:44
  • `Product1: "productname", Product1Price: 0.00` --- it is not array, and it is not hash =) ! Hash: `{"key_as_string": "Value"}` Array: `["String", 100]` – Zlatov Jan 04 '22 at 12:48
  • Can you provide actual code?. – Helpme Jan 04 '22 at 12:56
  • Scroll through the code please – Zlatov Jan 04 '22 at 12:59
  • Hey thanks for answer but instead of key can we have static number? Like product1id , product1name , product1price , product2id , product2price. – Helpme Jan 04 '22 at 13:43
  • It is difficult for me to understand your request and intentions. I've often come across a choice of how to store the data so that it's easy to get to it and loop over it. Here is a second example of how I would store the data, and how to access it. With explanations in the code. I recommend that you familiarize yourself with the basic javascript methods on your own. – Zlatov Jan 04 '22 at 17:49