【问题标题】:One array to four arrays一阵到四阵
【发布时间】:2021-04-30 15:04:30
【问题描述】:

我有一个这样的数组:

[{
    "coin": "AION",
    "profit": "3.10",
    "timestamp": "2021-01-26 00:48:01"
},
{
    "coin": "BTC",
    "profit": "77.00",
    "timestamp": "2021-01-26 00:08:04"
},
{
    "coin": "AION",
    "profit": "4.00",
    "timestamp": "2021-01-26 01:08:01"
},
{
    "coin": "BTC",
    "profit": "78.10",
    "timestamp": "2021-01-26 01:08:04"
}]

但我真正需要的是四个(四个是可变的)数组:

array coins:
[{ "AION", "BTC" }]
(variable with extra coins, depending on the first array i already have) > AION, BTC, ETH, ZIL, ARK, etc....

array profit[AION]:
[{ "3.10", "4.00" }]

array profit[BTC]:
[{ "77.00", "78.10" }]
(Variable extra coins/profits) [ETH, ZIL, ARK, etc...]

array timestamp:
[{ "2021-01-26 00:48","2021-01-26 01:08" }]
(Variable extra timestamps depending on first array i already have)

我需要这个来填充 am4charts.LineSeries 的 chartsData 数组。

有人可以帮帮我吗?或者有没有更好的选择?

【问题讨论】:

    标签: javascript arrays angularjs multidimensional-array amcharts


    【解决方案1】:

    这是一种问题,您需要遍历数组的元素并根据某些规则对某些内容进行分组。

    这是Array.reduce 的一个很好的用例(虽然你可以用其他方法做同样的事情)

    我会做这样的事情:

    function indexByCoins(coinsArray) {
        const coinsProfitByCoin = coinsArray.reduce((accumulator, coinInfo) => {
            const { coin, profit, timestamp } = coinInfo
    
            if (!accumulator[coin]) {
                accumulator[coin] = []
            }
    
            accumulator[coin].push({ profit, timestamp })
            return accumulator
        }, {})
    
        return coinsProfitByCoin
    }
    

    它并没有完全按照您的要求进行,而是将每个条目的利润与其时间戳分组。所以你会得到:

    {
      AION: [
        { profit: '3.10', timestamp: '2021-01-26 00:48:01' },
        { profit: '4.00', timestamp: '2021-01-26 01:08:01' }
      ],
      BTC: [
        { profit: '77.00', timestamp: '2021-01-26 00:08:04' },
        { profit: '78.10', timestamp: '2021-01-26 01:08:04' }
      ]
    }
    

    【讨论】:

      【解决方案2】:
      let arr=[{
          "coin": "AION",
          "profit": "3.10",
          "timestamp": "2021-01-26 00:48:01"
      },
      {
          "coin": "BTC",
          "profit": "77.00",
          "timestamp": "2021-01-26 00:08:04"
      },
      {
          "coin": "AION",
          "profit": "4.00",
          "timestamp": "2021-01-26 01:08:01"
      },
      {
          "coin": "BTC",
          "profit": "78.10",
          "timestamp": "2021-01-26 01:08:04"
      }]
      let timestampArr=[...new Set(arr.map(item=> item['timestamp']))]
      let coinArray=[...new Set(arr.map(item=> item['coin']))]
      let AionProfilt=[],btcProfit=[];
      arr.map(item=>{
        if(item.coin==='BTC')
          AionProfilt.push(item.profit)
         else
          btcProfit.push(item.profit)
      })
      console.log(coinArray,AionProfilt,btcProfit,timestampArr)
      

      【讨论】:

        【解决方案3】:

        您可以使用函数Array.prototype.reduce 进行分组和构建所需的输出。

        const array = [{    "coin": "AION",    "profit": "3.10",    "timestamp": "2021-01-26 00:48:01"},{    "coin": "BTC",    "profit": "77.00",    "timestamp": "2021-01-26 00:08:04"},{    "coin": "AION",    "profit": "4.00",    "timestamp": "2021-01-26 01:08:01"},{    "coin": "BTC",    "profit": "78.10",    "timestamp": "2021-01-26 01:08:04"}],
              result = array.reduce((a, {coin, profit, timestamp}) => {
                a.coins.push(coin);
                a.timestamps.push(timestamp);
                (a.profits[coin] || (a.profits[coin] = [])).push(profit);
        
                return a;
              }, {coins: [], profits: {}, timestamps: []});
        
        console.log(result);
        .as-console-wrapper { max-height: 100% !important; top: 0; }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多