0

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?

3 Answers3

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
    }
  ]
]
Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158
  • Will accept your answer in 9 min. – Philip Scroth Jan 21 '17 at 21:12
  • @PhilipScroth No problem. I've posted an update. Please play around with the JSFiddle as needed. – Raphael Rafatpanah Jan 21 '17 at 21:15
  • Maybe it's kinda bad idea to ask, but is it possible to get data by date? Basically it's gonna get first date, then get all values for the date and then 2nd date etc. – Philip Scroth Jan 21 '17 at 22:26
  • Do you control the structure of `config.json`? – Raphael Rafatpanah Jan 21 '17 at 22:41
  • Yes, I do. Correct. – Philip Scroth Jan 21 '17 at 22:47
  • Then instead of having `prices` be an array of arrays, make them an array of objects. I'll update my answer in a sec. – Raphael Rafatpanah Jan 21 '17 at 22:49
  • @PhilipScroth Updated. – Raphael Rafatpanah Jan 21 '17 at 23:12
  • Thank you so much! I wanted to ask, is it possible to get days "together"? I do a update every hour, but is it possible to put them together? Like May 27 then full data. May 28 then full data etc.. and if one is not yet completed then still put it together. – Philip Scroth Jan 21 '17 at 23:29
  • Yes, it's possible and not very difficult. Try this one on your own, and if you have trouble, post another question. The problem you have is that you have your data structured one way, and you want it to be structured a different way. If you are building this data from somewhere, then just structure it the way you want. Otherwise, you'll need to convert it. The structure you'll want is an array of objects, where each top level object's key is a Month/Day/Year (i.e. May 27 2015). Then you'll check if a month/day/year already exists. If so, append to it. If not, create it. – Raphael Rafatpanah Jan 21 '17 at 23:34
  • I've included an example data structure that describes the above in the answer. – Raphael Rafatpanah Jan 21 '17 at 23:39
  • Thanks for the help! :) – Philip Scroth Jan 21 '17 at 23:40
  • No problem. It's tough getting started. Just keep asking questions, even if you think you know the answer, and you'll hopefully learn a lot. Then, start trying to answer questions, and you'll learn more =) – Raphael Rafatpanah Jan 21 '17 at 23:51
0

Try replacing:

console.log(obj[i]);

with:

console.log(obj.prices[i]);
mrlew
  • 7,078
  • 3
  • 25
  • 28
0

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

for(var i = 0; i < obj.prices.length; i++){
  console.log(obj.prices[i]);
}
hackerrdave
  • 6,486
  • 1
  • 25
  • 29