Get the Last Item in an Array

Get the last item in an array

if (loc_array[loc_array.length - 1] === 'index.html') {
// do something
} else {
// something else
}

In the event that your server serves the same file for "index.html" and "inDEX.htML" you can also use: .toLowerCase().

Though, you might want to consider doing this server-side if possible: it will be cleaner and work for people without JS.


EDIT - ES-2022

Using ES-2022 Array.at(), the above may be written like this:

if (loc_array.at(-1) === 'index.html') {
// do something
} else {
// something else
}

Selecting last element in JavaScript array

How to access last element of an array

It looks like that:

var my_array = /* some array here */;
var last_element = my_array[my_array.length - 1];

Which in your case looks like this:

var array1 = loc['f096012e-2497-485d-8adb-7ec0b9352c52'];
var last_element = array1[array1.length - 1];

or, in longer version, without creating new variables:

loc['f096012e-2497-485d-8adb-7ec0b9352c52'][loc['f096012e-2497-485d-8adb-7ec0b9352c52'].length - 1];

How to add a method for getting it simpler

If you are a fan for creating functions/shortcuts to fulfill such tasks, the following code:

if (!Array.prototype.last){
Array.prototype.last = function(){
return this[this.length - 1];
};
};

will allow you to get the last element of an array by invoking array's last() method, in your case eg.:

loc['f096012e-2497-485d-8adb-7ec0b9352c52'].last();

You can check that it works here: http://jsfiddle.net/D4NRN/

How do I get the last element of a list?

some_list[-1] is the shortest and most Pythonic.

In fact, you can do much more with this syntax. The some_list[-n] syntax gets the nth-to-last element. So some_list[-1] gets the last element, some_list[-2] gets the second to last, etc, all the way down to some_list[-len(some_list)], which gives you the first element.

You can also set list elements in this way. For instance:

>>> some_list = [1, 2, 3]
>>> some_list[-1] = 5 # Set the last element
>>> some_list[-2] = 3 # Set the second to last element
>>> some_list
[1, 3, 5]

Note that getting a list item by index will raise an IndexError if the expected item doesn't exist. This means that some_list[-1] will raise an exception if some_list is empty, because an empty list can't have a last element.

Get the last item in an array

if (loc_array[loc_array.length - 1] === 'index.html') {
// do something
} else {
// something else
}

In the event that your server serves the same file for "index.html" and "inDEX.htML" you can also use: .toLowerCase().

Though, you might want to consider doing this server-side if possible: it will be cleaner and work for people without JS.


EDIT - ES-2022

Using ES-2022 Array.at(), the above may be written like this:

if (loc_array.at(-1) === 'index.html') {
// do something
} else {
// something else
}

JavaScript get index of last item in an array

In zero-based array systems, the index of the last item in an array is always the number of items of the array minus one.

In JS, you get the number of items in an array by calling the length property of the array object.

Then you subtract one from the length of the array, because JS arrays are zero-based.

const arr = ["one", "two", "three"];    
const arrLength = arr.length;

console.log(arrLength - 1); // expected output: 2 => length of array (3) minus 1

Access last element of a TypeScript array

You can access the array elements by it's index. The index for the last element in the array will be the length of the array-1 ( as indexes are zero based).

This should work.

var items: String[] = ["tom", "jeff", "sam"];

alert(items[items.length-1])

Here is a working sample.

Get the first and last item in an Array - JS

I've modified your code :

var myArray = ['Rodel', 'Mike', 'Ronnie', 'Betus'];

function firstAndLast(array) {

var firstItem = myArray[0];

var lastItem = myArray[myArray.length-1];

var objOutput = {

first : firstItem,

last : lastItem

};

return objOutput;

}

var display = firstAndLast(myArray);

console.log(display);


Related Topics



Leave a reply



Submit