数组中两个相邻数字之间的差异 [英] Difference between two adjacent numbers in array
问题描述
我开始冒险使用AngularJS,我有一个问题,是否可以计算两个数字之间的差异?
I start my adventure with AngularJS and I have a question, is it possible to calculate the difference between two numbers?
从SQL中提取数据后 - 我得到了一个像这样的表格,我希望在第1天< -2,2< -3等等之间获得访问之间的差异。
After extracting data from SQL - I get a table like this, I would like to receive a difference between visits on days 1 <- 2, 2 <-3, etc.
推荐答案
我认为它在简单的javascript范围内。
I think its in the scope of simple javascript.
如果您的数据包含在数组中,只需对其进行简单的迭代,并将当天访问与前一天的访问进行比较。
If you have your data in an array just use a simple iteration on it and compare the visit of current day to the visits from previous day.
一个非常简单的解决方案可能是:
A very simple solution might be:
var visitsArr = [38,29,18,29,28,18,24];
checkVisitsDiff(visitsArr);
function checkVisitsDiff(arr){
//Input validation check
if(!arr || arr.length <2){
throw "Bad input!";
}
//If input is ok - Start from second element
for(var i=1; i<arr.length; i++){
var currentElement = arr[i];
var previousElement = arr[i-1];
console.log("The difference from visit " + i + " to visit " +(i-1) + " is: " +(currentElement-previousElement));
}
}
我希望你提出这个问题。
I hope this what you asked.
这篇关于数组中两个相邻数字之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!