如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
您可以使用 Set 代替并使用删除函数 :
const s = Set;
s.add('hello');
s.add('goodbye');
s.delete('hello');
其他回答
用于解决多个浏览器的问题。 如果存在的话, 它会使用在建浏览器的方法。 如果没有这些方法, 则使用自己的自定义方法。 如果像旧版的互联网探索者一样, 它会使用自己的自定义方法 。
从数组中删除元素( 从网站) 的简单示例 :
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]
理解这一点:
您可以使用 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 矩阵项目来管理您的数据。 虽然没有单一的“ 撤销” 功能, 但您可以使用各种方式和战略清除不必要的阵列元素 。
您可以为此创建一个原型。只需通过数组元素和您想要从数组元素中删除的值:
Array. prototype. removeTrounds = 函数( array, val) { { 数组. (raryTrounds, index) {如果 (rayTroups = val) { 数组. splice( index, 1);} 返回数组;} var DummyAray = [1、2、 3、4、 5、 6]; 控制台.log (DummyArray. removeTround (DummyArray, 3) );
根据所有主要正确的答复并考虑到建议的最佳做法(特别是不直接使用Array.prototype),我提出了以下代码:
function arrayWithout(arr, values) {
var isArray = function(canBeArray) {
if (Array.isArray) {
return Array.isArray(canBeArray);
}
return Object.prototype.toString.call(canBeArray) === '[object Array]';
};
var excludedValues = (isArray(values)) ? values : [].slice.call(arguments, 1);
var arrCopy = arr.slice(0);
for (var i = arrCopy.length - 1; i >= 0; i--) {
if (excludedValues.indexOf(arrCopy[i]) > -1) {
arrCopy.splice(i, 1);
}
}
return arrCopy;
}
在审查上述功能时,尽管运作良好,但我意识到业绩可能有所改进。 使用ES6而不是ES5是一种更好的方法。
const arrayWithoutFastest = (() => {
const isArray = canBeArray => ('isArray' in Array)
? Array.isArray(canBeArray)
: Object.prototype.toString.call(canBeArray) === '[object Array]';
let mapIncludes = (map, key) => map.has(key);
let objectIncludes = (obj, key) => key in obj;
let includes;
function arrayWithoutFastest(arr, ...thisArgs) {
let withoutValues = isArray(thisArgs[0]) ? thisArgs[0] : thisArgs;
if (typeof Map !== 'undefined') {
withoutValues = withoutValues.reduce((map, value) => map.set(value, value), new Map());
includes = mapIncludes;
} else {
withoutValues = withoutValues.reduce((map, value) => { map[value] = value; return map; } , {});
includes = objectIncludes;
}
const arrCopy = [];
const length = arr.length;
for (let i = 0; i < length; i++) {
// If value is not in exclude list
if (!includes(withoutValues, arr[i])) {
arrCopy.push(arr[i]);
}
}
return arrCopy;
}
return arrayWithoutFastest;
})();
如何使用 :
const arr = [1,2,3,4,5,"name", false];
arrayWithoutFastest(arr, 1); // will return array [2,3,4,5,"name", false]
arrayWithoutFastest(arr, 'name'); // will return [2,3,4,5, false]
arrayWithoutFastest(arr, false); // will return [2,3,4,5]
arrayWithoutFastest(arr,[1,2]); // will return [3,4,5,"name", false];
arrayWithoutFastest(arr, {bar: "foo"}); // will return the same array (new copy)
我目前正在写博客文章, 我已将数个阵列解决方案设定为基准, 没有问题, 并比较运行时间 。 一旦我完成该文章, 我将更新此答案, 并使用链接 。 只需告知您, 我将以上内容与没有浏览器支持地图的 Lodash 比较, 以防浏览器支持地图, 它比 Lodash 还要快! 注意我没有使用 Array. prototype. indexof 或 Array. prototype 。 包含在地图或天体中将ExcludeValue 包装成快的查询 !
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;
}