NodeJS | Looping through a JSON gives undefined [duplicate]

441
January 22, 2017, at 2:39 PM

This question already has an answer here:

  • Access / process (nested) objects, arrays or JSON 11 answers

I have a piece of code here:

var obj = JSON.parse(fs.readFileSync('config.json', 'utf8'));
for(var i = 0; i < obj.prices.length; i++){
    console.log(obj[i]);
}

JSON: http://pastebin.com/6ZaVG4Xc

If I am getting the prices, it shows me undefined in the console. Why that?

Answer 1

You are not console.loging the right thing.

use: console.log(obj.prices[i]);

Update:

To get access each individual value (i.e. Date, Price, etc.), use the following:

for(var i = 0; i < obj.prices.length; i++){
    console.log('Date', obj.prices[i][0]);
    console.log('Price', obj.prices[i][1]);
    console.log('Amount', obj.prices[i][2]);
}

jsfiddle

Update 2:

It may be helpful to structure the prices data as an array of objects instead of an array of arrays. You can do things, like sort by date, either way, but you may find that it is easier to reason able an array of objects while also making it less error prone.

["Jun 02 2015 01: +0",3.931,"27070"]

{ "date": "Jun 02 2015 01: +0", "price": "3.931, "amount": "27070" }

Then you can sort based on date using a custom sorting function:

var sorted_prices = prices.sort(function(a, b){
  a = new Date(a.date),
  b = new Date(b.date);
  return a - b;
});

updated jsfiddle

If you wanted to keep your structure as an array of arrays, you would just pass a different value into new Date.

a = new Date(a[0]),
b = new Date(b[0]);

As a side note, when working with large data sets like these, regular expressions come in handy. If you don't already know regex, I converted your array of arrays to an array of objects with the following regex:

Find: \["(.*?)",(.*?),"(.*?)"\] Replace: {"date": "$1", "price": $2, "amount": $3}

To learn more about regex, I recommend this course.

Update:

var prices = [
  "May 27 2015": [
    {
      "price": 0.292,
      "amount": 888
    },
    {
      "price": 0.242,
      "amount": 118
    }
  ],
  "May 28 2015": [
    {
      "price": 0.492,
      "amount": 88228
    },
    {
      "price": 0.142,
      "amount": 1118
    }
  ]
]
Answer 2

Try replacing:

console.log(obj[i]);

with:

console.log(obj.prices[i]);
Answer 3

You aren't accessing the prices array in the loop:

for(var i = 0; i < obj.prices.length; i++){
  console.log(obj.prices[i]);
}
Rent Charter Buses Company
READ ALSO
Node.js server gives Cross-Origin Request Blocked

Node.js server gives Cross-Origin Request Blocked

My Nodejs server code is running on an AWS instance

719
How to scale backend Firebase service

How to scale backend Firebase service

I am building an application with a decent amount of Firebase service code running in the backendIt is possible that this App will end up reaching thousands and possibly hundreds of thousands of users

406
Rails server command JavaScript runtime error

Rails server command JavaScript runtime error

I just started learning Ruby on rails and everything works fine until when I run the server also I reinstalled Nodejs and no locki'm on windows 10

586