Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取索引处没有元素的数组_Javascript_Arrays - Fatal编程技术网

Javascript 获取索引处没有元素的数组

Javascript 获取索引处没有元素的数组,javascript,arrays,Javascript,Arrays,我实现了一个函数来获取数组的浅层副本,而不使用特定元素(使用其索引) 但我必须调用三种方法来实现这一点。有更好的方法吗 const arraywhithoutelementatindex=函数(arr,index){ 返回arr.slice(0,索引).concat(arr.slice(索引+1)) } 带有外部元素索引的数组([1,2,4,8,16,32,64],3)/[1,2,4,16,32,64] 您可以使用2个操作来完成,但看起来不太好看 “严格使用” const arrayWitho

我实现了一个函数来获取数组的浅层副本,而不使用特定元素(使用其索引)

但我必须调用三种方法来实现这一点。有更好的方法吗

const arraywhithoutelementatindex=函数(arr,index){
返回arr.slice(0,索引).concat(arr.slice(索引+1))
}
带有外部元素索引的数组([1,2,4,8,16,32,64],3)/[1,2,4,16,32,64]

您可以使用2个操作来完成,但看起来不太好看

“严格使用”
const arrayWithoutElementAtIndex=函数(arr,索引){
var ret=arr.slice();//复制
ret.splice(索引,1);//从给定索引中删除该项
return ret;//返回副本
}
var数组=[1,2,4,8,16,32,64];
var array2=arrayWithoutElementAtIndex(数组,3)/[1,2,4,16,32,64]
snippet.log(数组)
snippet.log(array2)

普通咖啡怎么样

const arraywhithoutelementatindex=函数(arr,index){
返回arr.filter(函数(值、arr索引){
返回索引!==返回索引;
});
}
document.write(带有outelementatindex([1,2,4,8,16,32,64,3]);//[1,2,4,16,32,64]
含lodash:

_(this.state.additionalOwners, index).splice(index, 1).value())

啊,是的!过滤器。。。哼!我希望es6实现了非胖箭头符号,或者至少提供了一个
fn
关键字。到处编写
函数
使得一行代码很难容纳80个字符。@用户1534422使用一个箭头,您可以这样做:
const arrayWithoutElementAtIndex=(arr,index,newArr=[…arr])=>(newArr.splice(index,1),newArr)
,您可以看到我的答案版本。请原谅我的英语代码高尔夫:
constfilter=(arr,index)=>arr.filter(((uu,i)=>index!==i)是的,我试着避免拼接,因为你总是会得到三倍多的线;)