简体   繁体   English

如何在数组JavaScript中获得最小或最大位置?

[英]How to get smallest or biggest position in array javascript?

How to get the smallest or biggest position in arrays javascript? 如何获得数组javascript中最小或最大的位置?

var cars = ["BMW", "Volvo", "Saab", "Ford", "Fiat", "Audi"];
var text = "";
var i;
for (i = 0; i < cars.length; i++) {
  text += cars[i] + "<br>";
} //I want to get car[i] (car[0])

How to get the smallest position "cars[0]" or biggest position "cars[5]" value??? 如何获得最小位置“ cars [0]”或最大位置“ cars [5]”的值??? Any tips, or code snippets are appreciated. 任何提示或代码段均表示赞赏。 Thanks. 谢谢。

cars[0] or cars[cars.length - 1] will get your values. cars[0]cars[cars.length - 1]将获取您的值。 But you can also use cars.shift() and cars.pop() to achieve the same, although these methods will alter the array. 但是,您也可以使用cars.shift()cars.pop()来实现相同的目的,尽管这些方法会改变数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM