我有这组数组,每组都有相同的长度,其中2个已经排序
我从这些变量开始: var sky = [0,3,4,5,6,7,8,9,10,11,12,14,16,17]; var ter = [];
我以这种方式分析每个天空项目: 对于每个天空物品我都能找到距离"天空项目和天空之间+ 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
所以天空阵列的第二个周期有相同的但是从天空阵列的第二个项开始,所以
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
我不知道如何计算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];
所以在下一阶段我可以参考ter数组
现在我只是一个开始并且没有完成,因为这只是一个尝试找到一个好点的开始,但我不知道为什么开始的事件不起作用,哈哈。在这种情况下,我不需要最后一个17,因为我对天空阵列上的最后一项没有任何意义
for (j = 0; j < sky.length; j++) {
if (j !== 0 || j !== sky.length){
ter.push(sky[j]-sky[0]);
}
}
console.log(ter);
有什么好主意吗?
答案 0 :(得分:0)
我认为在这种情况下递归比迭代更有效。
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));
<强>输出强>
[[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
如果您不熟悉ES2015,请参阅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));
答案 1 :(得分:0)
如果我理解正确的话,我猜你可能会这样做;
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));
或者可能是;
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 => f-e));
console.log(JSON.stringify(ters));
答案 2 :(得分:0)
首先,将您的问题减少为一个简单的功能:
/**
* @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];
}
现在,您可以查看:
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
然后,创建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);
希望这有帮助。
答案 3 :(得分:0)
您可以使用两个嵌套的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; }