Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  56] [ 5]  / answers: 1 / hits: 73462  / 8 Years ago, thu, september 15, 2016, 12:00:00

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


More From » arrays

 Answers
35

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




[#60709] Tuesday, September 13, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cierra

Total Points: 504
Total Questions: 108
Total Answers: 109

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
;