如何从数组中删除一个特定值? 类似 :

array.remove(value);

制约:我必须使用核心 JavaScript 。 框架不允许 。


当前回答

最干净的:

var arr = ['1','2','3']; arr = arr.filter (e'e? e?=======================================================================================================================================================3'3'); 控制台.warn(arr);

这也将删除重复(如果有的话)。

其他回答

业绩 业绩业绩 业绩业绩

今天(2019-12-09),我为选定的解决方案对macOS v10.13.6(高塞拉利昂)进行性能测试。我表示删除(A),但与其他方法相比,我没有使用,因为它在阵列中留下了空位。

结 结 结 结 结 结 结

最快的溶液是阵列。 splice (C) (对于大阵列、 阵列、 slice+splice (H) 是 Firefox 和 Safari 最快的不可改变的溶液 。 来自 (B) 的 Array. forest in Chrome mable 溶液通常比位于Safari 的小型表格的不可移动的快1.5x-6x, 令人惊讶的是, 变式溶(C) 慢于不可改变的溶液 (G) 。

详细细节

在测试中,我以不同的方式将中间元素从数组中去除。 A, C 的解决方案就位。 B, D, E, F, G, H 的解决方案是不可改变的 。

包含 10 元素的阵列结果

在铬中, 数组. spice (C) 是本地最快的解决方案。 数组. filter (D) 是最快的不可变的解决方案。 最慢的就是数组. sice (F)。 您可以在这里对您的机器进行测试 。

带有 1 000 000 元素的阵列结果

在铬中, 数组. spice (C) 是本地最快的解决方案( 删除( C) 的速率相似) - 但是在数组中留下了一个空位( 所以它不会执行“ 完全移除 ” ) 。 数组. sice- splice (H) 是最快的不可改变的解决方案。 最慢的就是数组. filter (D和E) 。 您可以在这里对您的机器进行测试 。

[var a = [0, 1, 2, 3, 4, 5, 6, 7, 9];var log = (letter, array) {(letter, trara.) log.log (leg. join `, `); 函数 A(arry) {var = trares[index]; {var (arr) {var = Array.(ar) 5; var. spice (al) = (aly) log (b) ; {var.log (ar) = listal. g(ary) = listal(5); trace. sloa. (var) log (var) = a. (l) a. (l) ; (var) lix (l) a. (l) a. (l) a. (l) ; (l) a. (l) a. (l) a. (l) a. (l) ex. (l) ex. (l) a. (l) a. (l) a. (l) a. (l) a. (l) a.

浏览器比较:Chrome v78.0.0,Safari v.13.0.4和Firefox v71.0.0

使用数组过滤法 :

let array = [1, 2, 3, 4, 511, 34, 511, 78, 88];

let value = 511;
array = array.filter(element => element !== value);
console.log(array)

您可以简单地将删除函数添加到数组原体Type

类似的东西 :

Array.prototype.remove = function(value) {
  return  this.filter(item => item !== value)
}

Array. prototype. remove = 函数( 值) { 返回此功能 。 filter (项目 项 ! === value) } 让阵列 = [10, 15,20] 结果 = 数组. remove(10) 控制台.log (结果) (结果)

根据所有主要正确的答复并考虑到建议的最佳做法(特别是不直接使用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 包装成快的查询 !

使用 indexof, 可以用 spolice 从数组中找到一个特定的数字索引, 从数组中删除一个特定的索引 。

const 数组 = [1,2,3,4,5,6,6,7,7,8,9,0]; const 指数 = 数组.indexof(5); /// 找到具体数字的索引 if( index != -1) { 数组.splice( index, 1); // 使用 index world. log( 数组) 删除数字 ;

删除全部事件 。 @ info: whatsthis

数组数组 = [1、2、3、4、4、5、1、7、8、9、9、2、3、4、5、6] 数组 = 数组. filter( number_@ number!=5); 控制台. log( 数组) ;

使用 join 和 share let 数组 = [1、2、3、4、5、1、7、8、9、2、3、4、5、6] 数组 = 数组 。 来自 (array.join ("-" -").split ("-5 -").join (" -").split ("-").split ("-"), number) 控制台.log (ray) 的 数组 。