Javascript 如何从数组中删除除javascript中的第一个元素之外的所有元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39510821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:38:44  来源:igfitidea点击:

How to remove all element from array except the first one in javascript

javascriptarraysslicearray-splice

提问by shadowhunter_077

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

我想从数组中删除除第 0 个索引处的数组元素之外的所有元素

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

Output should be a

输出应该是 a

回答by Satpal

You can set the lengthproperty of the array.

您可以设置length数组的属性。

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

OR, Use splice(startIndex)method

或,使用splice(startIndex)方法

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

OR use Array.slicemethod

或使用Array.slice方法

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

回答by Thank you

This is the headfunction. tailis also demonstrated as a complimentary function.

这就是head功能。tail也被证明是一种补充功能。

Note, you should only use headand tailon arrays that have a known length of 1or more.

请注意,您应该只在已知长度为1或更多的数组上使用head和。tail

// 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']

回答by Prateik Darji

array = [a,b,c,d,e,f];
remaining = array[0];
array = [remaining];

回答by Ravikiran kalal

You can use splice to achieve this.

您可以使用 splice 来实现这一点。

Input.splice(0, 1);

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

更多细节在这里。. . http://www.w3schools.com/jsref/jsref_splice.asp

回答by Mike Scotty

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

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

回答by eisbehr

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

如果要将其保留在 中array,可以使用slicesplice。或者再次包装第一个条目。

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

console.log( [Input[0]] );
console.log( Input.slice(0, 1) );
console.log( Input.splice(0, 1) );

回答by Evan Grillo

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

[input[0]];

// ["a"]

回答by Parthasarathy

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