如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
John Resig张贴了很好的执行:
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
如果您不想扩展一个全球对象, 可以做一些类似的东西,
// Array Remove - By John Resig (MIT Licensed)
Array.remove = function(array, from, to) {
var rest = array.slice((to || from) + 1 || array.length);
array.length = from < 0 ? array.length + from : from;
return array.push.apply(array, rest);
};
但我之所以张贴这篇文章,主要是为了提醒用户不要采取该页(2007年12月14日)评论中建议的其他实施方式:
Array.prototype.remove = function(from, to) {
this.splice(from, (to=[0, from || 1, ++to - from][arguments.length]) < 0 ? this.length + to : to);
return this.length;
};
它一开始似乎效果良好,但经过一个痛苦的过程,我发现它在试图删除数组中第二至最后一个元素时失败了。例如,如果您有一个 10 元素数组,并且试图用这个来删除第九元素:
myArray.remove(8);
我不知道为什么 但我确认约翰的原创执行没有问题
其他回答
理解这一点:
您可以使用 JavaScript 阵列来组合数值, 并在它们上面循环。 数组项目可以以多种方式被添加和删除。 总共有9种方法( 使用其中适合你的任何一种方法) 。 JavaScript 阵列不使用删除方法, 而是有多种方法可以清除数组值 。
这样做的不同方法:
您可以以这些方式从 JavaScript 阵列中删除元素 :
1-pop: 从阵列结尾处移除 。
2- Shift: 从数组的开头删除 。
3- 秒: 从特定的阵列索引中删除 。
4- 过滤器 : 允许您通过程序从矩阵中移除元素 。
方法 1: 从 JavaScript 阵列起始时删除元素
var ar = [“零 ” , “一 ” , “二 ” , “二 ” , “三 ” ; ar. shift (); ; // 返回“ 零” 控制台. log (ar ) ; / / [“ 1 ” , “ 2 ” , “ 三 ” ]
方法2:从 JavaScript 阵列结束时删除元素
var ar ar = [1、2、3、4、5、6]; ar = 4; // 设定删除元素控制台的长度; log( ar ); / / [ 1、 2、 3、 4] var ar ar = [ 1、 2、 3、 4、 5、 6] ; ar.pop (); / 返回 6 lador. log ( ar ); / [ 1、 2、 3、 4、 5]
方法 3: 在 JavaScript 中使用串列来移除队列元素
var arr = [1、2、3、4、5、6、7、8、9、10] ; var = rr.spice(2,2,2); laporal.log(arr); var list = [“巴”、“巴”、“巴”、“巴”、“瓦兹”、““foo”、“qux”; 列表.spice(0, 2); laft.log(list);
方法 4 : 使用 Splice 按值删除队列项目
var arr = [1、2、3、4、4、5、6、7、8、9、10]; for( var i = 0; i < arr. long; i++){如果( rr) = 5 { rr.spice(i, 1);} { rr.spice( y, 1);} { { [1, 2, 3, 4, 4, 6, 7, 8, 9, 9, 10];f( var i = 0; i < arr. lon; i++){ 如果 (rr) = 5 { arr.spice(i, 1); i-;} } /[1, 2, 3, 4, 6, 7, 8, 9, 10]
方法5:使用矩阵过滤法按值删除项目
var 数组 = [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10] ; var 过滤 = 数组. filter( 功能( 值、 索引、 rr) { 返回值 > 5; } // filter { [6, 7, 8, 9] / filter { [1、2, 3, 4, 5, 6, 7, 8, 9, 10]
方法6: Lodash 阵列清除方法
var 数组 = [1, 2, 3, 4] ; var evens =_.remove( 数组, 函数(n) { 返回 n%% 2 { { 0;} ); 控制台. log( 数组); / / { {[ 1, 3] 等式. log( events); / { { {[ 2, 4]
方法7:制定方法
var 数组 = [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10]; 函数数组 remove( rr, value) { 返回 arr. filter( ele) { 返回 ele yel = value;}} var 结果 = tratremove( 数组, 6); / 结果 = [1, 2, 3, 4, 5, 7, 8, 9, 10]
方法 8 : 使用 删除运算符明确删除矩阵元素
var ar = [1, 2, 3, 4, 4, 5, 6]; 删除 ar[4]; // 删除含有索引 4 控制台的元素 。 log( ar ); / [ 1, 2, 3, 4, 未定义, 6] 警报( ar ); / 1, 2, 3,4,6
方法 9: 清除或重置 JavaScript 阵列
var ar = [1、2、3、4、4、5、6];//do staffar = [];//a new, 空阵列;vararar1 = [1、2、3、4、5、6];varar2 = arr1;/ 参考rr1 由另一个变量ar1 = [;控制台(rr2);/ 输出[1、2、3、4、4、5、6] rr1 = [1、2、3、4、5、6] ;varar2 = arr1;// 参考arr1 =另一个变量ar1 = [;控制台(rr2);/ 输出[1、2、3、4、5、6]
摘要:
关键是要通过删除 JavaScript 矩阵项目来管理您的数据。 虽然没有单一的“ 撤销” 功能, 但您可以使用各种方式和战略清除不必要的阵列元素 。
更新: 只有当您无法使用 ECMAScript 2015 (前称ES6) 时, 才会推荐使用此方法。 如果您可以使用此方法, 其它答案可以提供更清晰的落实 。
此条格将解决您的问题, 并删除所有参数, 而不是仅一个( 或指定值 ) 。
Array.prototype.destroy = function(obj){
// Return null if no objects were found and removed
var destroyed = null;
for(var i = 0; i < this.length; i++){
// Use while-loop to find adjacent equal objects
while(this[i] === obj){
// Remove this[i] and store it within destroyed
destroyed = this.splice(i, 1)[0];
}
}
return destroyed;
}
用法 :
var x = [1, 2, 3, 3, true, false, undefined, false];
x.destroy(3); // => 3
x.destroy(false); // => false
x; // => [1, 2, true, undefined]
x.destroy(true); // => true
x.destroy(undefined); // => undefined
x; // => [1, 2]
x.destroy(3); // => null
x; // => [1, 2]
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;
}
答案已经很多了, 但是因为还没有人用一个衬里来做, 我想我会展示我的方法。 它会利用字符串. split () 函数在创建数组时将删除所有指定字符这一事实。 这里举一个例子 :
var ary = [1、2、3、4、1234、10、4、5、7、3]; out = arry.join (" -" -").split ("-4 -").join (" -").split (" -").split (" -");control.log(out) ;
在此示例中, 所有 4 个的字符都在从数组中移除 。 但是, 必须指出, 包含字符“ - ” 的任何数组都会与此示例产生问题 。 简而言之, 这会导致组合( “ - ” ) 函数不适当地将您的字符串拼凑在一起。 在这种情况下, 上面的扇形中的所有“ - ” 字符串都可以替换为在原始数组中不会使用的任何字符串 。 以下还有一个示例 :
var ary = [1,2,3,4,'-',1234,10,'-',4,5,7,3]; out = ary.join("!@#").split("!@#4!@#").join("!@#").split("!@#"); console.log(out);
我对JavaScript很新,需要这个功能。我只是写了这个:
function removeFromArray(array, item, index) {
while((index = array.indexOf(item)) > -1) {
array.splice(index, 1);
}
}
然后,当我想使用它:
//Set-up some dummy data
var dummyObj = {name:"meow"};
var dummyArray = [dummyObj, "item1", "item1", "item2"];
//Remove the dummy data
removeFromArray(dummyArray, dummyObj);
removeFromArray(dummyArray, "item2");
输出 - 如预期的那样。 [“项目1”、“项目1”]
你可能跟我有不同的需要,所以你可以轻易地修改,以适合他们。我希望这能帮助别人。