214

const array = ["item 1", "item 2", "item 3", "item 4"];

// removes the first element of the array, and returns that element.
console.log(array.shift());
// prints "item 1"

//removes the last element of the array, and returns that element.
console.log(array.pop());
// prints "item 4"

  1. How to remove the first array but return the array minus the first element?

  2. In my example I should get "item 2", "item 3", "item 4" when I remove the first element?

8
  • 18
    alert(array.slice(1)) or array.shift(); alert(array); Commented Jun 29, 2016 at 10:18
  • 6
    @Thomas when i use myarray.shift() returns "item 1" what i want is return "item 2", "item 3", "item 4" Commented Jun 29, 2016 at 10:19
  • Please read the whole code I wrote, using shift() Commented Jun 29, 2016 at 10:21
  • 2
    If you need to create new array without first element then use slice. Commented Jun 29, 2016 at 10:22
  • 8
    Just to add one more to the mix :) With destructuring: [,...myarray] = myarray; Commented Jun 29, 2016 at 10:25

10 Answers 10

295

The .shift() method removes the first element of an array. Then you can return the remaining:

const array = ["item 1", "item 2", "item 3", "item 4"];
array.shift();

console.log(array);

As others have suggested, you could also use slice(1):

const array = ["item 1", "item 2", "item 3", "item 4"];
  
console.log(array.slice(1));

Sign up to request clarification or add additional context in comments.

2 Comments

Note that slice(1) doesn't "remove the first element" from the array. Instead, it creates a new array with shallow copies of all of the values from the array except the first.
Useful to use slice(1) if you have an array of arrays.
100

Why not use ES6?

const array = ["item 1", "item 2", "item 3", "item 4"];
const [, ...rest] = array;

console.log(rest)

4 Comments

I like this idea, except that first it's going to be an unused constant.
Can fix that using const [,...rest] = myarray =O =D
Love this because it does not modifiy the initial array ref and it makes the PR reviewer go ¯_(ツ)_/¯
How does this work sir ??
8

Try this:

const array = ["item 1", "item 2", "item 3", "item 4"];

// removes the first element of the array, and returns that element apart from item 1.
array.shift();
console.log(array);

1 Comment

did you try this one?see my updated question with snippet it will return "item 1"
7

Use:

array.slice(1, array.length);

as follows:

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const array2 = array.slice(1, array.length); //arrayExceptfirstValue

console.log(array2);

Comments

4

myarray.splice(1) will remove the first item from the array … and return the updated array (['item 2', 'item 3', 'item 4'] in your example).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Comments

4

This can be done in one line with lodash _.tail:

const array = ["item 1", "item 2", "item 3", "item 4"];

console.log(_.tail(array));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Comments

1

The easiest way I can think of is this:

const array = ["item 1", "item 2", "item 3", "item 4"];
const [, ...arrayYouNeed] = array;

console.log(arrayYouNeed);

The original array is untouched and you can use the arrayYouNeed wherever you need it.

If you wanna know how it works look up 'deconstructing arrays'!

Comments

0

If you do not want to modify the array, you can either slice or filter out by index.

const array = [1, 2, 3, 4];

console.log(array.filter((_, i) => i !== 0));
console.log(array.slice(1));
console.log(array);

Comments

0

Just for fun :)

We can create our function to remove at any index and get rest array:

const array = ["item 1", "item 2", "item 3", "item 4"];

const removeNthElement = (removeIndex, arr) => {
  if (removeIndex < 0 || removeIndex >= array.length) return [...arr];

  const result = Array(arr.length - 1);

  for (let i = 0; i < removeIndex; ++i) {
    result[i] = arr[i];
  }

  for (let i = removeIndex + 1; i < arr.length; ++i) {
    result[i - 1] = arr[i];
  }

  return result;
}

// Testing

for (let i = -1; i <= array.length; ++i) {
  console.log(removeNthElement(i, array));
}

Comments

-3

You can use array.slice(0,1) // First index is removed and array is returned.

3 Comments

FIrst index is not removed, a copy is created without the first element. The original array is not modified.
Also, slice(0,1) will return an array containing the first item only. Instead you need to call slice(1) to get an array containing all but the first item, as in the accepted answer
You probably meant array.splice(0,1), not "slice". Note the "p" letter. As a side note, I consider this a bad naming convention by js where two methods differ by just 1 letter and both methods give you sort of opposite results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.