我想做什么
if(data){
var loop = var i = 0; i < arr.length; i++; // ASC
} else {
loop = var i = arr.length-1; i > -1; i--; // DSC
}
for(loop){ ..content.. }
我该怎么做?
答案 0 :(得分:1)
如果您要对数组进行排序,请查看.sort:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
public static List<MyBean> GetRecords(String query){
List<MyBean> myBeans = new ArrayList<>();
//Executing query and saving result into result set
try {
stmt = con.createStatement();
result = stmt.executeQuery(query);
while (result.next()) {
MyBean myBean = new MyBean();
// Populate the bean...
myBeans.add(myBean);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return myBeans;
}
如果您要从头开始而不是从头开始遍历整个数组,请签出.reverse:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
const arr = [4, 2, 5, 1, 3];
const data = true;
if (data) {
arr.sort((a, b) => a - b); // asc
} else {
arr.sort((a, b) => b - a); // desc
}
console.log(arr)
在JavaScript中,您很少(尽管可以)使用标准的for循环,而是将.forEach,.reduce等迭代器与.reverse,.concat等数组函数结合使用。