【问题标题】:json object to array, node.jsjson 对象到数组,node.js
【发布时间】:2020-03-22 08:35:45
【问题描述】:

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

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

【问题讨论】:

    标签: javascript json node.js


    【解决方案1】:

    运算符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);

    【讨论】:

      【解决方案2】:

      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 用于普通的旧数组。

      【讨论】:

      • 也可以使用Object.values(CurrentPair)
      【解决方案3】:

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

      你想要的是

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

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

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

      【讨论】:

      • 它受支持,但它并没有像 OP 认为的那样做。
      • 没错,我的措辞不是最好的。
      【解决方案4】:

      你几乎做对了。但是在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);

      因为CurrentPairarray,您也可以简单地使用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);

      【讨论】:

        【解决方案5】:

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

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

        但是,请参阅Why is using "for...in" with array iteration a bad idea?

        【讨论】:

          【解决方案6】:
          //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)}})
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-03-31
            • 2018-11-13
            • 2011-01-10
            • 2015-08-27
            • 1970-01-01
            • 2015-02-22
            • 2016-05-08
            • 1970-01-01
            相关资源
            最近更新 更多