25

I want to remove all element from array except the element of array at 0th index

["a", "b", "c", "d", "e", "f"]

Output should be a

Mohammad
  • 19,228
  • 14
  • 49
  • 73
shadowhunter_077
  • 325
  • 1
  • 5
  • 12
  • `var output = Input[0];` – Mairaj Ahmad Sep 15 '16 at 12:19
  • 2
    Or `Input.slice().splice(1)` if you actually want an array. And if you don’t care about the original array, use `Input.splice(1)`. – Sebastian Simon Sep 15 '16 at 12:20
  • okay my requirement is like that i have to implement a functionality called remove tab which remove all the tab from tabset array except the home tab which is the tab at 0th index. – shadowhunter_077 Sep 15 '16 at 12:24
  • Are these "tabs" actually some HTML elements? – Teemu Sep 15 '16 at 12:26
  • Possible duplicate of [How to remove a particular element from an array in JavaScript?](http://stackoverflow.com/questions/5767325/how-to-remove-a-particular-element-from-an-array-in-javascript) – aledustet Sep 15 '16 at 12:47
  • i don't know but splice(0,1) is not working for me it remove home tab(tab at 0th index) from tabset which is not desirable. – shadowhunter_077 Sep 15 '16 at 12:50

8 Answers8

62

You can set the length property of the array.

var input = ['a','b','c','d','e','f'];  
input.length = 1;
console.log(input);

OR, Use splice(startIndex) method

var input = ['a','b','c','d','e','f'];  
input.splice(1);
console.log(input);

OR use Array.slice method

var input = ['a','b','c','d','e','f'];  
var output = input.slice(0, 1) // 0-startIndex, 1 - endIndex
console.log(output); 
abhirathore2006
  • 2,807
  • 22
  • 29
Satpal
  • 126,885
  • 12
  • 146
  • 163
4

This is the head function. tail is also demonstrated as a complimentary function.

Note, you should only use head and tail on arrays that have a known length of 1 or more.

// head :: [a] -> a
const head = ([x,...xs]) => x;

// tail :: [a] -> [a]
const tail = ([x,...xs]) => xs;

let input = ['a','b','c','d','e','f'];

console.log(head(input)); // => 'a'
console.log(tail(input)); // => ['b','c','d','e','f']
Thank you
  • 107,507
  • 28
  • 191
  • 224
1
array = [a,b,c,d,e,f];
remaining = array[0];
array = [remaining];
Prateik Darji
  • 2,154
  • 1
  • 9
  • 27
1

You can use splice to achieve this.

Input.splice(0, 1);

More details here . . .http://www.w3schools.com/jsref/jsref_splice.asp

Ravikiran kalal
  • 652
  • 5
  • 11
1

You can use slice:

var input =['a','b','c','d','e','f'];  
input = input.slice(0,1);
console.log(input);

Documentation: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

Mike Scotty
  • 8,981
  • 5
  • 28
  • 42
1

If you want to keep it in an array, you can use slice or splice. Or wrap the wirst entry again.

var Input = ["a","b","c","d","e","f"];  

console.log( [Input[0]] );
console.log( Input.slice(0, 1) );
console.log( Input.splice(0, 1) );
eisbehr
  • 11,374
  • 7
  • 30
  • 56
-1
var input = ["a", "b", "c", "d", "e", "f"];

[input[0]];

// ["a"]
  • 3
    Hey, you should explain your answer a bit. What were your thoughts behind it? As well may firstly fully write our answer before posting it ;) – WolverinDEV Apr 30 '20 at 18:57
  • @WolverinDEV Could you explain the downvote? I came across this post and read that two of the top solutions used splice & slice. I provided a solution with the least amount of characters required... – Evan Grillo May 15 '20 at 18:50
-2
var output=Input[0]

It prints the first element in case of you want to filter under some constrains

var Input = [ a, b, c, d, e, a, c, b, e ];
$( "div" ).text( Input.join( ", " ) );

Input = jQuery.grep(Input, function( n, i ) {
  return ( n !== c );
});
Parthasarathy
  • 178
  • 1
  • 10