【问题标题】:Access position in JavaScript Array with last of particular number使用特定数字的最后一个访问 JavaScript 数组中的位置
【发布时间】:2017-12-14 00:33:47
【问题描述】:

我有一个打开的 websocket 向我传递数据,数据是一个对象,其中包括一个时间戳(日期类型)和价格(数字类型)。

* 我需要在每分钟结束时访问最后的价格。 *

所以我在日期上使用了 .getMinute() 方法,它返回分钟。 例如,如果日期是 2017-12-14T00:26:19.944Z 并且价格是 19.99,那么我将创建一个像这样的对象:{time: 26, price: 19.99}

然后我将它推送到一个数组中并重复该过程... ...所以我可以有一个这样的数组:

[{time: 26, price: 19.99}, {time: 26, price: 17.99}, {time: 26, price: 22.45}, {time: 27, price: 25.21}]

我需要访问每个 time 属性的最后价格 - 所以上面我需要访问 22.45 的价格并将其推送到一个新数组中。

帮助感谢,谢谢。

【问题讨论】:

    标签: javascript arrays sorting datetime object


    【解决方案1】:

    您可以利用objects 不能具有相同名称的重复属性这一事实。

    请参阅下面的实际示例。

    注意使用reduce() 函数到达object form {time: price} 和随后的map() 函数返回原始array form [{time, price}..]

    结果是一个array,只包含每个minutelatest price

    // Original Data.
    const data = [{time: 26, price: 19.99}, {time: 26, price: 17.99}, {time: 26, price: 22.45}, {time: 27, price: 25.21}]
    
    // Object Form.
    const unique = data.reduce((accumulator, point) => ({...accumulator, [point.time]: point.price}), {})
    
    // Array Form.
    const array = Object.keys(unique).map((time) => ({time, price: unique[time]}))
    
    // Result.
    console.log('Object Form', unique) // Object Form.
    console.log('Array Form', array) // Array Form.

    【讨论】:

      【解决方案2】:

      最简单的方法就是返回数组值的最后一个元素

      let lastPrice = data[data.length - 1].price;
      

      下面的例子

      let data = [{time: 26, price: 19.99}, {time: 26, price: 17.99}, {time: 26, price: 22.45}, {time: 27, price: 25.21}]
      
      let lastPrice = data[data.length - 1].price;
      console.log(lastPrice);

      【讨论】:

        【解决方案3】:

        类似这个简单的函数应该可以工作:

        var getLatestTimestamp = function(arr){
            var lastTimestamps = [];
            var l = arr.length -1 ;
            	
            lastTimestamps.push(arr[l]);
            for(var i = l; i > 0; i--){
            	if(lastTimestamps[lastTimestamps.length -1].time !== arr[i].time) {
            		lastTimestamps.push(arr[i]);
            	}
            }
            
            return lastTimestamps;
        };
            
        var arr = [
            {time: 26, price: 19.99}, 
            {time: 26, price: 17.99}, 
            {time: 26, price: 22.45}, 
            {time: 27, price: 25.21},
        ];
            
        var distinctTimestamps = getLatestTimestamp(arr);
            
        console.log(distinctTimestamps); 
        /* 
        [ { time: 27, price: 25.21 }, { time: 26, price: 22.45 } ]
        */

        【讨论】:

          【解决方案4】:

          可能是这样的

          let newArr = [timeArr[timeArr.lenght - 1].price]
          or
          otherArr.push(timeArr[timeArr.lenght - 1].price)
          

          【讨论】:

            猜你喜欢
            • 2018-08-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-05-12
            • 2021-03-05
            • 2021-12-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多