如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
我设定了一个函数:
function pop(valuetoremove, myarray) {
var indexofmyvalue = myarray.indexOf(valuetoremove);
myarray.splice(indexofmyvalue, 1);
}
并用它这样:
pop(valuetoremove, myarray);
其他回答
检查一下这个代码 每个主要浏览器都使用这个代码
删除项目 = 函数( rr) { varb = ' ; 对于 (b in arr) { 如果 (ar[ b] { { { { { rr. splice( b, 1); 折断 ; } rr; } 返回 arr; } } 返回 arr; } ; var 数组 = [1, 5,6, 9, 5, 5, 355] var res = mote_ item( arry, 5); 控制台. log( res) = [1, 3, 5, 9, 5, 5, 55, Var Res = dection_ item( arry, 5); 控制台 log( res)
使用 jQuery 的阵列 :
A = [1, 2, 3, 4, 5, 6];
A.splice($.inArray(3, A), 1);
//It will return A=[1, 2, 4, 5, 6]`
注意: 如果元素未找到, 在阵列中返回 - 1 。
我张贴我的代码,删除一个阵列元素, 并缩短阵列长度 。
function removeElement(idx, arr) {
// Check the index value
if (idx < 0 || idx >= arr.length) {
return;
}
// Shift the elements
for (var i = idx; i > 0; --i) {
arr[i] = arr[i - 1];
}
// Remove the first element in array
arr.shift();
}
Vanilla JavaScript(ES5.1) - 已建立版本
浏览器支持: Internet Explorer 9 或后( 详细浏览器支持)
/**
* Removes all occurences of the item from the array.
*
* Modifies the array “in place”, i.e. the array passed as an argument
* is modified as opposed to creating a new array. Also returns the modified
* array for your convenience.
*/
function removeInPlace(array, item) {
var foundIndex, fromIndex;
// Look for the item (the item can have multiple indices)
fromIndex = array.length - 1;
foundIndex = array.lastIndexOf(item, fromIndex);
while (foundIndex !== -1) {
// Remove the item (in place)
array.splice(foundIndex, 1);
// Bookkeeping
fromIndex = foundIndex - 1;
foundIndex = array.lastIndexOf(item, fromIndex);
}
// Return the modified array
return array;
}
Vanilla JavaScript(ES5.1) - 永恒版本
浏览器支持: 与原版的香草 JavaScript 相同
/**
* Removes all occurences of the item from the array.
*
* Returns a new array with all the items of the original array except
* the specified item.
*/
function remove(array, item) {
var arrayCopy;
arrayCopy = array.slice();
return removeInPlace(arrayCopy, item);
}
Vanilla ES6 - 永恒版本
浏览器支持: Chrome 46, 边缘 12, Firefox 16, Opera 37, Safari 8 (详细浏览器支持)
/**
* Removes all occurences of the item from the array.
*
* Returns a new array with all the items of the original array except
* the specified item.
*/
function remove(array, item) {
// Copy the array
array = [...array];
// Look for the item (the item can have multiple indices)
let fromIndex = array.length - 1;
let foundIndex = array.lastIndexOf(item, fromIndex);
while (foundIndex !== -1) {
// Remove the item by generating a new array without it
array = [
...array.slice(0, foundIndex),
...array.slice(foundIndex + 1),
];
// Bookkeeping
fromIndex = foundIndex - 1;
foundIndex = array.lastIndexOf(item, fromIndex)
}
// Return the new array
return array;
}
我想根据ECMAScript 6. 假设你的阵列如下:
let arr = [1,2,3,4];
如果您想要删除像 2 这样的特殊索引, 请写下以下代码 :
arr.splice(2, 1); //=> arr became [1,2,4]
但如果您想要删除一个特殊项目, 如 3 和您不知道它的索引, 做如下 :
arr = arr.filter(e => e !== 3); //=> arr became [1,2,4]
提示 : 请使用箭头函数进行过滤回击, 除非您会得到空数组 。