Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript json对象到数组,node.js_Javascript_Json_Node.js - Fatal编程技术网

Javascript json对象到数组,node.js

Javascript json对象到数组,node.js,javascript,json,node.js,Javascript,Json,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", "

我有一个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.
}

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

中的操作符
返回对象的属性,在您的示例中,它返回该数组的索引

var数组=[{“打开”:“0.06353900”,“高”:“0.06354800”,“低”:“0.06341700”,“关闭”:“0.06347300”,“卷”:“335.48500000”,“时间戳”:1521640800000},{“打开”:“0.06347300”,“高”:“0.06365400”,“低”:“0.06344000”,“关闭”:“0.06357500”,“卷”:“461.02800000”,“时间戳”:1521641100000},{“打开”:“0.06349500”,“高”:“0.06360400”,“低”:“0.06341400”,“关闭”:“0.06352300”,“音量”:“495.5060000”,“时间戳”:1521641400000}];
var lowTotal=[];
用于(数组中的项){
lowTotal.push(数组[项].low);
}

console.log(lowtottal);
x in y
在对象的属性名称上循环

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

为此,您需要
x/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
for…in
JavaScript中的数组不支持循环

你想要的是

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


您做得几乎正确。但是在
for…in
循环中,您将在对象内部收到属性
name
索引。因此您需要使用
CurrentPair
来获取所需信息

var数据='[{“打开”:“0.06353900”,“高”:“0.06354800”,“低”:“0.06341700”,“关闭”:“0.06347300”,“卷”:“335.48500000”,“时间戳”:1521640800000},{“打开”:“0.06347300”,“高”:“0.06365400”,“低”:“0.06344000”,“关闭”:“0.06357500”,“卷”:“461.02800000”,“时间戳”:1521641100000},{“打开”:“0.06349500”,“高”0.06365400”,“低”:“0.06400”:0.06341400,“收盘”:“0.06352300”,“成交量”:“495.5060000”,“时间戳”:1521641400000}];
var CurrentPair=JSON.parse(数据);
var lowTotal=[];
for(当前对中的var项){
lowTotal.push(当前对[项目].low);
}

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

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

但是,see也可以使用
对象。值(CurrentPair)
它是受支持的,但它并不像OP所认为的那样。没错,我的措辞不是最好的。
CurrentPair.forEach(item => { ... });
for (item in CurrentPair) {
    lowTotal[item] = CurrentPair[item].low;
}
//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)}})