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

array.remove(value);

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


当前回答

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

其他回答

例如,您有一个字符数组,想要从数组中删除“A”。

数组有一个过滤方法,可以过滤并只返回您想要的元素。

let CharacterArray = ['N', 'B', 'A'];

我想返回元素 除了"A"。

CharacterArray = CharacterArray.filter(character => character !== 'A');

字符阵列必须是 : ['N', 'B']

我想根据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]

提示 : 请使用箭头函数进行过滤回击, 除非您会得到空数组 。

Array. prototype. removeByValue = 函数( val) {, 对于 (var i = 0; i < this. long; i++) { 如果 (i) val) { this. splice (i, 1); i- ; } 返回此 ;} var falts = [apple, 'banana', 'carrot', 'orange' ] ; falves.remove ByValue ('banana'); 礼服. log (fruts); / / - > [apple, 'carrot', 'orange' ]

通过我的解决方案, 您可以在纯 JavaScript 的阵列中删除一个或多个项。 不需要另外的 JavaScript 库 。

var myArray = [1,2,3,4,5]; // First array

var removeItem = function(array,value) {  // My clear function
    if(Array.isArray(value)) {  // For multi remove
        for(var i = array.length - 1; i >= 0; i--) {
            for(var j = value.length - 1; j >= 0; j--) {
                if(array[i] === value[j]) {
                    array.splice(i, 1);
                };
            }
        }
    }
    else { // For single remove
        for(var i = array.length - 1; i >= 0; i--) {
            if(array[i] === value) {
                array.splice(i, 1);
            }
        }
    }
}

removeItem(myArray,[1,4]); // myArray will be [2,3,5]

如果您必须支持旧版的 Internet Explorer , 我建议使用以下多个填充( 注意: 这不是一个框架 ) 。 这是所有现代阵列方法( JavaScript 1. 8. 5/ ECMAScript 5 Array Extras) 的100%的后向兼容替换, 用于 Internet Explorer 6+, Firefox 1. 5+, Chrome, Safari, & Opera 。

https://github.com/pludude/array-generics https://github.com/pludude/ array-generics https://github.com/plusdude/arary-generics/servics/