我想从数组中删除除第0个索引的数组元素
之外的所有元素["a", "b", "c", "d", "e", "f"]
输出应为a
答案 0 :(得分:28)
您可以设置数组的0) Desktop 1) Documents 2) Downloads
3) Music 4) Pictures 5) Public
6) Templates 7) XXX 8) Videos
9) backup_XXXX 10) dna 11) scripting
12) sessions 13) sh 14) temp
属性。
length
或,使用splice(startIndex)
方法
var input = ['a','b','c','d','e','f'];
input.length = 1;
console.log(input);
答案 1 :(得分:4)
这是head
函数。 tail
也被证明是一种免费功能。
注意,您只应在已知长度 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']
答案 2 :(得分:1)
如果您想将其保留在array
中,可以使用slice
或splice
。或者再次包装wirst条目。
var Input = ["a","b","c","d","e","f"];
console.log( [Input[0]] );
console.log( Input.slice(0, 1) );
console.log( Input.splice(0, 1) );

答案 3 :(得分:0)
array = [a,b,c,d,e,f];
remaining = array[0];
array = [remaining];
答案 4 :(得分:0)
答案 5 :(得分:0)
您可以使用切片:
var input =['a','b','c','d','e','f'];
input = input.slice(0,1);
console.log(input);
文档:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
答案 6 :(得分:-1)
let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
request.HTTPMethod = "PATCH"
request.addValue("\(token)", forHTTPHeaderField: "authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
do{
let bodyData = try NSJSONSerialization.dataWithJSONObject(["email":"nuv@gmail.com"], options: [])
request.HTTPBody = bodyData
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
if let mdata = data {
let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
print(contents)
} else {
print(error?.localizedDescription)
}
}
}catch{
print("failed serialization")
}
如果您想在某些约束条件下进行过滤,它会打印出第一个元素
var output=Input[0]
答案 7 :(得分:-1)
var input = ["a", "b", "c", "d", "e", "f"];
[input[0]];
// ["a"]