繁体   English   中英

json 对象到数组,node.js

[英]json object to array, node.js

我有一个 ETHBTC.json 文件:

[
{
    "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
}
]

我正在尝试遍历数据并将所有“低”值保存到数组中

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

控制台日志给了我未定义的值。 任何帮助都会很棒,谢谢

运算符in返回对象的属性,在您的情况下返回该数组的索引。

 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);

可能,您想使用的是for-of-loop

这种for-loop循环在数组上循环,每次迭代都返回一个特定的元素/对象。

 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);

您可以使用reduce或简单的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);

x in y遍历对象的属性名称

您试图将x视为属性的,而不是名称。

为此,您需要x of y

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

不过,使用map会更惯用。

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

旁白:在 JavaScript 中,约定为构造函数保留以大写字母开头的变量名。 不要将名称CurrentPair用于普通的旧数组。

JavaScript 中的数组不支持for... in循环。

你想要的是

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

要么

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

要么

CurrentPair.forEach(item => { ... });

你几乎做对了。 但是在for... in循环中,您将收到对象内部的属性nameindex 所以你需要使用CurrentPair来获取你想要的信息。

 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);

因为CurrentPair是一个array ,你也可以简单地使用forEach

 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);

item是数组索引,而不是数组值。 lowTotal[item]中使用它时可以正确使用它,但在尝试读取low属性时却不能。 它应该是:

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

但是,请参阅为什么将“for...in”与数组迭代一起使用是个坏主意?

//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)}})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM