我有一个像这样的JavaScript代码:
var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for (var i = 0, di = 1; i >= 0; i += di) {
if (i == myArray.length - 1) { di = -1; }
document.writeln(myArray[i]);
}
我需要它在中间停止,如10,从10开始倒数到0回。
到目前为止,我已经设法让它从0到20和20 - 0。
如何在中间停止并从那里开始呢?
请帮助任何人!
答案 0 :(得分:1)
将数组长度除以2
+------------+-----------------+----------------------+
| time | rtt | rtt_mips |
+------------+-----------------+----------------------+
| 1521731100 | NULL | NULL |
| 1521731400 | NULL | NULL |
| 1521731700 | 12593 | 0.04197666666666667 |
| 1521732000 | NULL | NULL |
| 1521732300 | NULL | NULL |
| 1521732600 | NULL | NULL |
| 1521732900 | 41266.90234375 | 0.13755633333333334 |
| 1521733200 | NULL | NULL |
| 1521733500 | NULL | NULL |
| 1521733800 | NULL | NULL |
| 1521734100 | NULL | NULL |
| 1521734400 | NULL | NULL |
| 1521734700 | NULL | NULL |
| 1521735000 | 14979.439453125 | 0.049931333333333335 |
| 1521735300 | 11812.119140625 | 0.03937366666666667 |
| 1521735600 | NULL | NULL |
| 1521735900 | 8738.2314453125 | 0.02912743333333333 |
| 1521736200 | NULL | NULL |
| 1521736500 | NULL | NULL |
| 1521736800 | NULL | NULL |
+------------+-----------------+----------------------+
20 rows in set (0.12 sec)
答案 1 :(得分:1)
这是一个使用函数的示例,该函数接受数组以及要向前和向后显示的项目数:
var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
if(myArray.length === 1){
ShowXElementsForwardsAndBackwards(myArray, 1);
}
else if(myArray.length === 0) {
//Do nothing as there are no elements in array and dividing 0 by 2 would be undefined
}
else {
ShowXElementsForwardsAndBackwards(myArray, (myArray.length / 2));
}
function ShowXElementsForwardsAndBackwards(mYarray, numberOfItems){
if (numberOfItems >= mYarray.length) {
throw "More Numbers requested than length of array!";
}
for(let x = 0; x < numberOfItems; x++){
document.writeln(mYarray[x]);
}
for(let y = numberOfItems - 1; y >= 0; y--){
document.writeln(mYarray[y]);
}
}
答案 2 :(得分:0)
Array.reverse()可以帮助你解决这个问题吗?
const array = [0,1,3,4,5,6,7,8,9,10,11,12,13,14,15]
const getArrayOfAmount = (array, amount) => array.filter((item, index) => index < amount)
let arraySection = getArrayOfAmount(array, 10)
let reversed = [...arraySection].reverse()
console.log(arraySection)
console.log(reversed)
然后你可以使用你想要的水刀阵列操作为每个阵列“做事”。
答案 3 :(得分:0)
难道你不能检查一下你是否已经完成了它,然后从长度中减去你当前的位置?
for(i = 0; i <= myArray.length; i++){
if( Math.round(i/myArray.length) == 1 ){
document.writeln( myArray[ myArray.length - i] );
} else {
document.writeln( myArray[i] );
}
}
除非我遗漏了什么?
答案 4 :(得分:0)
如果捕获中点(数组长度的一半),只需向相反方向开始步骤。
const N = 20;
let myArray = [...Array(N).keys()];
let midpoint = Math.round(myArray.length/2)
for ( let i=1, step=1; i; i+=step) {
if (i === midpoint)
step *= -1
document.writeln(myArray[i])
}
&#13;
为了让事情更清楚,我已经:
i
)为1;这也意味着数组在0
索引处有一个未使用的0
值;换句话说,myArray[0]==0
从未显示i
,这意味着当i==0
循环将停止,因为它是 falsy di
重命名为step
,这与其他术语更为一致midpoint
使用Math.round()
确保它是最高整数(中点)(例如,15/2 == 7.5
,但您希望它为8)midpoint
是一个变量;计算循环体中的中点是多余的,效率较低,因为它只需要计算一次N
...
)。但这很容易避免答案 5 :(得分:0)
您可以将检查移动到for循环的条件块中。
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
for (
var i = 0, l = (myArray.length >> 1) - 1, di = 1;
i === l && (di = -1), i >= 0;
i += di
) {
document.writeln(myArray[i]);
}
&#13;