Brownman Revival
Brownman Revival

Reputation: 3850

How can I remove the first element of an array and return the rest?

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?

Upvotes: 214

Views: 285967

Answers (10)

EzioMercer
EzioMercer

Reputation: 2005

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));
}

Upvotes: 0

ginalx
ginalx

Reputation: 2247

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);

Upvotes: 0

I&#39;m Geeker
I&#39;m Geeker

Reputation: 4637

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);

Upvotes: 7

Penny Liu
Penny Liu

Reputation: 17428

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>

Upvotes: 4

ziggelflex
ziggelflex

Reputation: 33

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'!

Upvotes: 1

Najmul Hoq
Najmul Hoq

Reputation: 213

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);

Upvotes: 7

Tudor Morar
Tudor Morar

Reputation: 3868

Why not use ES6?

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

console.log(rest)

Upvotes: 100

Jesper H&#248;jer
Jesper H&#248;jer

Reputation: 3192

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));

Upvotes: 292

bryn
bryn

Reputation: 81

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

Upvotes: 4

Hassan Abbas
Hassan Abbas

Reputation: 1316

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

Upvotes: -3

Related Questions