-2

I have a ETHBTC.json file :

[
{
    "open": "0.06353900",
    "high": "0.06354800",
    "low": "0.06341700",
    "close": "0.06347300",
    "volume": "335.48500000",
    "timestamp": 1521640800000
},
{
    "open": "0.06347300",
    "high": "0.06365400",
    "low": "0.06344000",
    "close": "0.06357500",
    "volume": "461.02800000",
    "timestamp": 1521641100000
},
{
    "open": "0.06349500",
    "high": "0.06360400",
    "low": "0.06341400",
    "close": "0.06352300",
    "volume": "495.50600000",
    "timestamp": 1521641400000
}
]

I'm trying to loop through the data and save all of the 'low' values to an array

const fs = require('fs');
var data = fs.readFileSync('charts/ETHBTC.json');
var CurrentPair = JSON.parse(data);


var lowTotal = [];

for(item in CurrentPair){
lowTotal[item] = item.low;
console.log(lowTotal[item]); //put this just to check if working.
}

the console log is giving me undefined values. Any help would be great, thanks

tel90
  • 9
  • 1
  • 7

6 Answers6

2

The operator in returns the properties of an object, in your case is returning the indexes of that array.

var array = [{    "open": "0.06353900",    "high": "0.06354800",    "low": "0.06341700",    "close": "0.06347300",    "volume": "335.48500000",    "timestamp": 1521640800000},{    "open": "0.06347300",    "high": "0.06365400",    "low": "0.06344000",    "close": "0.06357500",    "volume": "461.02800000",    "timestamp": 1521641100000},{    "open": "0.06349500",    "high": "0.06360400",    "low": "0.06341400",    "close": "0.06352300",    "volume": "495.50600000",    "timestamp": 1521641400000}];

var lowTotal = [];
for(item in array){
  lowTotal.push(array[item].low);
}

console.log(lowTotal);

Probably, what you want to use is a for-of-loop

This kind of for-loop loops over the array and for each iteration returns a specific element/object.

var array = [{    "open": "0.06353900",    "high": "0.06354800",    "low": "0.06341700",    "close": "0.06347300",    "volume": "335.48500000",    "timestamp": 1521640800000},{    "open": "0.06347300",    "high": "0.06365400",    "low": "0.06344000",    "close": "0.06357500",    "volume": "461.02800000",    "timestamp": 1521641100000},{    "open": "0.06349500",    "high": "0.06360400",    "low": "0.06341400",    "close": "0.06352300",    "volume": "495.50600000",    "timestamp": 1521641400000}];

var lowTotal = [];
for(item of array){
  lowTotal.push(item.low);
}

console.log(lowTotal);

You can use either reduce or a simple forEach.

var array = [{    "open": "0.06353900",    "high": "0.06354800",    "low": "0.06341700",    "close": "0.06347300",    "volume": "335.48500000",    "timestamp": 1521640800000},{    "open": "0.06347300",    "high": "0.06365400",    "low": "0.06344000",    "close": "0.06357500",    "volume": "461.02800000",    "timestamp": 1521641100000},{    "open": "0.06349500",    "high": "0.06360400",    "low": "0.06341400",    "close": "0.06352300",    "volume": "495.50600000",    "timestamp": 1521641400000}];

var result = array.reduce((a, {low}) => [...a, low], []);

console.log(result);
Ele
  • 33,468
  • 7
  • 37
  • 75
1

x in y loops over the property names of the object.

You are trying to treat x as the values of the properties, not the names.

For that you need x of y.

for(item of CurrentPair){
    lowTotal[item] = item.low;
    console.log(lowTotal[item]); //put this just to check if working.
}

It would be more idiomatic to use map though.

var lowTotal = CurrentPair.map(item => item.low);
console.log(lowTotal);

Aside: Convention, in JavaScript, reserves variable names starting with a capital letter for constructor functions. Don't use the name CurrentPair for a plain old array.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

for ... in loops are not supported by arrays in JavaScript.

What you want is

for (const item of CurrentPair) { ... }

or

for (let i = 0; i < CurrentPair.length; i++) {
    const item = CurrentPair[i];
}

or

CurrentPair.forEach(item => { ... });
Alex Harley
  • 353
  • 1
  • 3
  • 13
0

You did it nearly right. But in a for ... in loop, you will receive the property name or the index inside the object. So you need to use CurrentPair to get the information you want.

var data = '[{"open": "0.06353900","high": "0.06354800","low": "0.06341700","close": "0.06347300","volume": "335.48500000","timestamp": 1521640800000},{"open": "0.06347300","high": "0.06365400","low": "0.06344000","close": "0.06357500","volume": "461.02800000","timestamp": 1521641100000},{"open": "0.06349500","high": "0.06360400","low": "0.06341400","close": "0.06352300","volume": "495.50600000","timestamp": 1521641400000}]';

var CurrentPair = JSON.parse(data);
var lowTotal = [];

for (var item in CurrentPair) {
  lowTotal.push(CurrentPair[item].low);
}

console.log(lowTotal);

Because CurrentPair is an array, you could simply use forEach too:

var data = '[{"open": "0.06353900","high": "0.06354800","low": "0.06341700","close": "0.06347300","volume": "335.48500000","timestamp": 1521640800000},{"open": "0.06347300","high": "0.06365400","low": "0.06344000","close": "0.06357500","volume": "461.02800000","timestamp": 1521641100000},{"open": "0.06349500","high": "0.06360400","low": "0.06341400","close": "0.06352300","volume": "495.50600000","timestamp": 1521641400000}]';

var CurrentPair = JSON.parse(data);
var lowTotal = [];

CurrentPair.forEach(function(item) {
  lowTotal.push(item.low);
});

console.log(lowTotal);
eisbehr
  • 12,243
  • 7
  • 38
  • 63
0

item is the array index, not the array value. You use it correctly when using it in lowTotal[item], but not when you try to read the low property. It should be:

for (item in CurrentPair) {
    lowTotal[item] = CurrentPair[item].low;
}

However, see Why is using "for...in" with array iteration a bad idea?

Barmar
  • 741,623
  • 53
  • 500
  • 612
0
//I don't know what u are exactly trying to do. This may help


//push all low values in an array

const lowTotal =CurrentPair.map((pair)=>pair.low);    

 or 

//push all low values as an array of object

const lowTotal =CurrentPair.map((pair)=> {return {low:pair.low}});


//if you want to sum all low values then follow this approach

const lowTotal =CurrentPair.map((pair)=>pair.low).reduce((pre,nex)=>Number(pre)+Number(nex));

or

const lowTotal=CurrentPair.map(pair=>{return{low:pair.low}}).reduce((pre,nex)=> {return {low:Number(pre.low)+ Number(nex.low)}})
Rayees
  • 57
  • 3
  • 15