[英]Javascript find distance between items on array
I've this set of array, each with the same length and 2 of this are sorted我有这组数组,每个数组的长度相同,其中 2 个已排序
I start with this vars: var sky = [0,3,4,5,6,7,8,9,10,11,12,14,16,17];我从这个变量开始:var sky = [0,3,4,5,6,7,8,9,10,11,12,14,16,17]; var ter = [];
变量 = [];
I've to analyze every sky item in that way: for each sky items I've to find the "distance" between the sky item and the sky+1, then from sky item and sky+1+1 etc etc... so我必须以这种方式分析每个天空项目:对于每个天空项目,我必须找到天空项目和天空+1 之间的“距离”,然后从天空项目和天空+1+1 等...所以
var ter = 3-0
var ter = 4-0
var ter = 5-0
var ter = 6-0
var ter = 7-0
var ter = 8-0
var ter = 9-0
var ter = 10-0
var ter = 11-0
var ter = 12-0
var ter = 14-0
var ter = 16-0
var ter = 17-0
So the second cycle for the sky array have todo the same but start with the second items on sky array so will be所以天空阵列的第二个周期必须做同样的事情但是从天空阵列上的第二个项目开始所以将是
var ter = 4-3
var ter = 5-3
var ter = 6-3
var ter = 7-3
var ter = 8-3
var ter = 9-3
var ter = 10-3
var ter = 11-3
var ter = 12-3
var ter = 14-3
var ter = 16-3
var ter = 17-3
I don't know how to calculate the ter var, and at this point maybe the best is to have it in array, like that我不知道如何计算 ter var,在这一点上也许最好的办法是将它放在数组中,就像那样
ter = [[3,4,5,6,7,8,9,10,11,12,14,16,17],[1,2,3,4,5,6,7,8,9,11,13,14], and so on];
so in the next phase I can refer to the ter array所以在下一阶段我可以参考 ter 数组
For now I've only the start and is not complete because is only a start to try to find a good point, but I don't know why event to start don't works, lol.现在我只是开始,还没有完成,因为这只是一个开始,试图找到一个好的点,但我不知道为什么开始的事件不起作用,大声笑。 Ps I don't need the last one the 17 in this case, because I don't have nothing over the last items on sky array
Ps 在这种情况下,我不需要最后一个 17,因为我在 sky array 上的最后一个项目什么都没有
for (j = 0; j < sky.length; j++) {
if (j !== 0 || j !== sky.length){
ter.push(sky[j]-sky[0]);
}
}
console.log(ter);
Any quick idea?任何快速的想法?
I think a recursion is more efficient than iteration in this case. 我认为在这种情况下,递归比迭代更有效。
JavaScript (ES2015) JavaScript(ES2015)
let dist = (a,r = []) => {
if(r.length <= a.length-2) {
let t = [];
let b = a[r.length];
a.forEach(e => t.push(e - b));
r.push(t.filter(e => e > 0));
return dist(a,r);
} else return r;
}
let sky = [0,3,4,5,6,7,8,9,10,11,12,14,16,17];
let ter = [];
console.log(dist(sky,ter));
Output 产量
[[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 17], [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 14], [1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 13], [1, 2, 3, 4, 5, 6, 7, 9, 11, 12], [1, 2, 3, 4, 5, 6, 8, 10, 11], [1, 2, 3, 4, 5, 7, 9, 10], [1, 2, 3, 4, 6, 8, 9], [1, 2, 3, 5, 7, 8], [1, 2, 4, 6, 7], [1, 3, 5, 6], [2, 4, 5], [2, 3], [1]]
[[3、4、5、6、7、8、9、10、11、12、14、16、17],[1、2、3、4、5、6、7、8、9、11 13、14],[1、2、3、4、5、6、7、8、10、12、13],[1、2、3、4、5、6、7、9、11、12] ,[1、2、3、4、5、6、8、10、11],[1、2、3、4、5、7、9、10],[1、2、3、4、6, 8、9],[1、2、3、5、7、8],[1、2、4、6、7],[1、3、5、6],[2、4、5],[ 2,3],[1]]
JS Bin: http://jsbin.com/muhade/edit?js,console JS Bin: http : //jsbin.com/muhade/edit? js, console
If you're not familiar with ES2015, here's the same code in ES5:- 如果您不熟悉ES2015,则以下是ES5中的相同代码:-
JavaScript (ES5) JavaScript(ES5)
var dist = function (a, r) {
r = r || [];
if (r.length <= a.length - 2) {
var t = [];
var b = a[r.length];
a.forEach(function (e) { return t.push(e - b); });
r.push(t.filter(function (e) { return e > 0; }));
return dist(a, r);
}
else return r;
};
var sky = [0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 17];
var ter = [];
console.log(dist(sky, ter));
I guess you might do as follows if i have understood correctly; 如果我理解正确,我想您可能会执行以下操作;
var sky = [0,3,4,5,6,7,8,9,10,11,12,14,16,17], ters = sky.map((e,i,a) => a.slice(i+1).map(f => [e,f])) .reduce((p,c) => p.concat(c)); console.log(JSON.stringify(ters));
Or may be like; 或者可能像;
var sky = [0,3,4,5,6,7,8,9,10,11,12,14,16,17], ters = sky.map((e,i,a) => a.slice(i+1).map(f => fe)); console.log(JSON.stringify(ters));
First, reduce your problem to one simple function : 首先,将您的问题简化为一个简单的函数 :
/**
* @param {Number[]} sky - The original sky array
* @param {Number} cycle - First cycle is 1, second is 2, so on..
* @param {Number} itemIndex - Index of item to take distance to
*/
function distance(sky, cycle, itemIndex) {
cycle = cycle - 1; // the first cycle is actually 0
if (cycle < 0) return Number.NaN;
if (cycle > sky.length - 2) return Number.NaN;
if (itemIndex > sky.length - cycle - 1) return Number.NaN;
return sky[itemIndex + cycle + 1] - sky[cycle];
}
Now, for instance, you can check: 现在,例如,您可以检查:
var sky = [0,3,4,5,6,7,8,9,10,11,12,14,16,17];
console.log(distance(sky, 1, 7)); // 10
console.log(distance(sky, 2, 7)); // 8
Then, to create ter
: 然后,创建
ter
:
var sky = [0,3,4,5,6,7,8,9,10,11,12,14,16,17];
var ter = [];
for (var cycle = 0 ; cycle < sky.length - 1 ; cycle++) {
var innerTer = [];
for (var itemIndex = 0; itemIndex <= sky.length - cycle - 1 ; itemIndex++)
innerTer.push(distance(sky, cycle, itemIndex));
ter.push(innerTer);
}
console.log(ter);
Hope this helps. 希望这可以帮助。
You could use two nested for
loops and push the difference to new array. 您可以使用两个嵌套的
for
循环,然后将差值推送到新数组。
var sky = [0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 17], i, j, ter = []; for (i = 0; i < sky.length - 1; i++) { ter[i] = []; for (j = i + 1; j < sky.length; j++) { ter[i].push(sky[j] - sky[i]); } } console.log(ter);
.as-console-wrapper { max-height: 100% !important; top: 0; }
A simple way would be:一个简单的方法是:
const getDistance = (arr, numA, numB) => numB - (arr.indexOf(numA)-1)
Then:然后:
const items = [1,2,3,4,5]
const distance = getDistance(items, 2, 4)
console.log(distance) // 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.