I have an array in javascript. I have to find last value of that array. My code is like
var array= fruits;
and I have to find last fruit in that array.
Since in JavaScript arrays of size n
are indexed from [0..n-1]
, you can get the last element by simply indexing the array at n-1
, where is the length of the array and can be obtained by the .length
property:
var lastFruit = array[array.length - 1];
If you want to get the last element and also remove it from the array, you can use the .pop
methid:
var lastFruit = array.pop(); // get last element of array and remove it from array
Note: As RobG stated:
length
is at least one more than the highest index, but might be greater
First find number of elements in array and store it on variable like
var total = array.length;
Now you can find last value of array using below code
var last = array[total-1];
You accomplish this by using the length of the array (fruits.length) and subtracting 1 (because the array is 0 indexed). For instance...
fruits[0]="pear";
fruits[1]="apple";
fruits[2]="peach";
So fruits.length would be 3, so fruits.length - 1 = 2. So to get the last element...
var lastFruit = fruits[fruits.length-1];
I have to find last value of that array
The length property is at least one higher than the highest index, however it might be higher in which case the n-1 member may not exist. Eg
// Create an array with length 10
var a = new Array(10);
// Add one member at index 0
a[0] = 'foo';
So the "last" value of the array is at index 0 and has a value of "foo". So you can start with length - 1 and search backwards for members that actually exist:
function lastValue(arr) {
var i = arr.length;
while (i--) {
if (i in arr) {
return arr[i];
}
}
// If get to here, arr has no members
return -1;
}
// All the following arrays have length 4 but different number of members
console.log(lastValue([ , , , ,])); // -1, i.e. there are no members
console.log(lastValue([0,1, , ,])); // 1
console.log(lastValue([0,1,2,3 ])); // 3
console.log(lastValue([ , , ,3 ])); // 3
If you know the Array only has numeric members (ie there are no non-numeric properties and no negative properties) you can do:
var arr = [,,'foo',,'bar',,]; // length 6, highest member is at index 4
console.log(arr[Math.max.apply(Math, Object.keys(arr))]); // 'bar'
Requires ES5 Object.keys .
好吧,您可以找到它的长度,然后知道最后一个索引,并可以从中获取项目
var last_item = array[array.length - 1];
它就像:
fruits[fruits.length-1]
Multiple ways to find last value of an array in javascript
Without affecting original array
Modifies original array
By creating own helper method
var arr = [1,2,3,4,5]; console.log(arr.slice(-1)[0]) console.log(arr[arr.length-1]) const [last] = [...arr].reverse(); console.log(last) console.log(arr.reverse()[0]) console.log('----------Modifies original array----------'); var arr2 = [5, 10, 15]; console.log(arr2.pop()) arr2.push(15) console.log(...arr2.splice(-1)); console.log('----------By creating own helper method------'); let arr3 = [2, 4, 6, 8, 10]; Object.defineProperty(arr3, 'last', { get: function(){ return this[this.length-1]; } }) console.log(arr3.last);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.