0

I have created json array like this,

data = [{ product_id: 1, manufacturer: 'man1', product: 'test1' },
        { product_id: 2, manufacturer: 'man2', product: 'test2' }]

I want to list this data in tabular format without using key name. I have tried like,

for(i=0; i<data.length;i++){
    console.log(data[i].product_id+","+data[i].product+","+data[i].manufacturer);
}

Its working fine. But I want to display values without specifying key name like, .product_id,.product etc..Is it possible in javascript? Could you please support me to resolve this?

Mark
  • 90,562
  • 7
  • 108
  • 148
  • 4
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Oct 06 '18 at 05:07
  • What do you mean you want to do it without specifying a key name? That's the typical way of getting the value. What are you trying to achieve? – Herohtar Oct 06 '18 at 05:19

3 Answers3

4

You can use Object.values() to get an iterator of all the values of the object. For example to log them all to the console separated by commas without specifying each key you could:

let data = [
  { product_id: 1, manufacturer: 'man1', product: 'test1' },
  { product_id: 2, manufacturer: 'man2', product: 'test2' }
]

for(i=0; i<data.length;i++){
    console.log(Object.values(data[i]).join(','))
}

This will list the objects own property names, so it won't follow the prototype chain up (if that matters).

EDIT based on comment (using padEnd() to specify a fixed width):

let data = [
    { product_id: 1, manufacturer: 'man1', product: 'test1' },
    { product_id: 2, manufacturer: 'man2', product: 'test2' }
  ]

let fixedWidth = 7
for(i=0; i<data.length;i++){
    let padded = Object.values(data[i])
    .map(str => String(str).padEnd(fixedWidth, " "))
    .join('')
    console.log(padded)
 }
Mark
  • 90,562
  • 7
  • 108
  • 148
  • can we specify width between each values? – Rugma Manas Oct 06 '18 at 05:38
  • @RugmaManas, I'm not sure what you are trying to do. It's a little unusual to specify layout when writing to the console, but you could `map` each value to a string and use `string.padStart` or `padEnd` (see edit) – Mark Oct 06 '18 at 05:47
0

You can use the for in statement to iterate over an object, like this:

data = [{ product_id: 1, manufacturer: 'man1', product: 'test1' },
        { product_id: 2, manufacturer: 'man2', product: 'test2' }]
for(object of data){
  for(key in object){
    console.log(object[key])
  }
}
FZs
  • 16,581
  • 13
  • 41
  • 50
0

You could also do something like this:

var data = [{ product_id: 1, manufacturer: 'man1', product: 'test1' }, { product_id: 2, manufacturer: 'man2', product: 'test2' } ]

const log = (padLength) => data.forEach(x => console.log(Object.values(x).join(' '.repeat(padLength))))

log(2)
log(5)

Using forEach since data is already an array as well as Object.values to get the values in an array followed by a join which uses the padLength parameter to repeat the number of empty spaces.

Akrion
  • 18,117
  • 1
  • 34
  • 54