使用javascript找到每日比较的差异

时间:2017-08-28 08:00:18

标签: javascript momentjs

[{date:'2017-8-1',count: 100},
{date:'2017-8-2',count: 200},
{date:'2017-8-3',count: 101},
{date:'2017-8-4',count: 123},
{date:'2017-8-5',count: 14},
{date:'2017-8-6',count: 12},
{date:'2017-8-7',count: 2},
{date:'2017-8-8',count: 3}] //today

我有8个数组项,我想找到每天的差异。但我不知道如何计算前一天的差异。例如,2017-8-8与2017-8-7之间的差异是1,我该如何进行循环?

3 个答案:

答案 0 :(得分:0)

您可以使用var data = [{date:'2017-8-1',count: 100}, {date:'2017-8-2',count: 200}, {date:'2017-8-3',count: 101}, {date:'2017-8-4',count: 123}, {date:'2017-8-5',count: 14}, {date:'2017-8-6',count: 12}, {date:'2017-8-7',count: 2}, {date:'2017-8-8',count: 3}]; var result = []; data.forEach(function(obj, idx) { if(!idx) return; result.push(data[idx].count - data[idx-1].count); }); console.log(result);来计算数组中两个相邻计数值之间的差异,如下所示:

        //If player clicks centre on first move go in corner square
        if (current[4] === playerToken && this.state.stepNumber === 1) {
          let move = cornerSquares[Math.floor(Math.random() * cornerSquares.length)];
          drawSquare(move);
        } 
        //If player clicks corner square on first move go in centre square
        else if (this.state.stepNumber === 1) {
          for (let i = 0; i < cornerSquares.length; i++){
            if (current[cornerSquares[i]] === playerToken) {
              drawSquare(4);
            }
          }
        }
        //If player or computer has 2 in a row, place in 3rd square to win or block
        else if (/*CONDITION OF THE BELOW FOR LOOP*/) {
          for (let i = 0; i < twoInRow.length; i++) {
            const [a, b, c] = twoInRow[i];  
            if (current[a] && current[a] === current[b]) {
              drawSquare(c);
            }
          }
        }
        //Place in random empty square
        else {
         //code to randomly place x/o in random square
        }
      }

答案 1 :(得分:0)

您可以执行new Date("2017-8-8").getDate()

我希望这会对你有所帮助:)。

var input = [{date:'2017-8-1',count: 100},
{date:'2017-8-2',count: 200},
{date:'2017-8-3',count: 101},
{date:'2017-8-4',count: 123},
{date:'2017-8-5',count: 14},
{date:'2017-8-6',count: 12},
{date:'2017-8-7',count: 2},
{date:'2017-8-8',count: 3}];
for(var i = input.length-1; i>0; i--){
   var dateDiff = new Date(input[i].date).getDate() - new Date(input[i-
   1].date).getDate();
   //pushing the diff in input array
   input[i].diffFromPrevEntry = dateDiff;
}
console.log(input)

答案 2 :(得分:0)

这是一个计算差异的代码段。

var dataArray = [
{date:'2017-8-1',count: 100},
{date:'2017-8-2',count: 200},
{date:'2017-8-3',count: 101},
{date:'2017-8-4',count: 123},
{date:'2017-8-5',count: 14},
{date:'2017-8-6',count: 12},
{date:'2017-8-7',count: 2},
{date:'2017-8-8',count: 3}
];

function calculateDifferences(data) {
  var result = [];
  
  // Start at index 1 instead of 0 so we immediately have a previous item.
  for (var index=1; index<data.length; index++) {
  // Get the item at the current and previous index.
      var previousItem = data[index - 1],
          currentItem = data[index];
    // Push an object into the result array with the start and 
    // end date, as well as the difference between the count of the two days.
    result.push({
      startDate: previousItem.date,
      endDate: currentItem.date,
      difference: currentItem.count - previousItem.count
    });    
  }
  
  // Return the array with the result.
  return result;
}

// Log the result of the method.
console.log(calculateDifferences(dataArray));