我想从数组中删除除第0个索引处的数组元素以外的所有元素
["a", "b", "c", "d", "e", "f"]
输出应为a
您可以设置数组的length
属性。
这是var input = ['a','b','c','d','e','f'];
input.splice(1);
console.log(input);
功能。 head
也被证明是一项补充功能。
// 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']
您可以使用拼接来实现。
您可以使用切片:
如果要将其保留在https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/slice中,则可以使用array
或slice
。或再次包装愿望条目。
var Input = ["a","b","c","d","e","f"];
console.log( [Input[0]] );
console.log( Input.slice(0, 1) );
console.log( Input.splice(0, 1) );
var input = ["a", "b", "c", "d", "e", "f"];
[input[0]];
// ["a"]