Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first and last value from array

Tags:

javascript

I have a an array being passed to me which is something like

var arr = ["A", "B", "C"];

What I am trying to achieve is to only get the first and last value so it should look like

var arr = ["A", "C"];

I'm not how to achieve this using splice because when I do

arr.splice(I am not sure what numbers to put here).forEach(function (element) {
        console.log(element);
});

Can someone tell me how to achieve this please.

like image 816
Izzy Avatar asked Jan 29 '23 08:01

Izzy


1 Answers

What I am trying to achieve is to only get the first and last value so it should look like

Simply

arr.splice( 1, arr.length - 2 );

Demo

var arr = ["A", "B", "C"];

arr.splice(1, arr.length - 2);

console.log(arr);
like image 150
gurvinder372 Avatar answered Jan 31 '23 22:01

gurvinder372