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

array.remove(value);

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


当前回答

我不知道你是怎么想的array.remove(int)行为。我可以想到三种可能性 你可能想要。

在索引中删除数组的元素i:

array.splice(i, 1);

如果您想要删除带有值的每个元素number从数组 :

for (var i = array.length - 1; i >= 0; i--) {
 if (array[i] === number) {
  array.splice(i, 1);
 }
}

如果您只想在索引中生成元素i不再存在,但你不希望其它元素的索引改变:

delete array[i];

其他回答

ES6且无突变:(2016年10月)

const removeByIndex = (list, index) =>
      [
        ...list.slice(0, index),
        ...list.slice(index + 1)
      ];
         
output = removeByIndex([33,22,11,44],1) //=> [33,11,44]
      
console.log(output)

我刚刚创建了一个多填充Array.prototype通过Object.defineProperty以删除数组中一个想要的元素,而不会在稍后通过for .. in ..

if (!Array.prototype.remove) {
  // Object.definedProperty is used here to avoid problems when iterating with "for .. in .." in Arrays
  // https://stackoverflow.com/questions/948358/adding-custom-functions-into-array-prototype
  Object.defineProperty(Array.prototype, 'remove', {
    value: function () {
      if (this == null) {
        throw new TypeError('Array.prototype.remove called on null or undefined')
      }

      for (var i = 0; i < arguments.length; i++) {
        if (typeof arguments[i] === 'object') {
          if (Object.keys(arguments[i]).length > 1) {
            throw new Error('This method does not support more than one key:value pair per object on the arguments')
          }
          var keyToCompare = Object.keys(arguments[i])[0]

          for (var j = 0; j < this.length; j++) {
            if (this[j][keyToCompare] === arguments[i][keyToCompare]) {
              this.splice(j, 1)
              break
            }
          }
        } else {
          var index = this.indexOf(arguments[i])
          if (index !== -1) {
            this.splice(index, 1)
          }
        }
      }
      return this
    }
  })
} else {
  var errorMessage = 'DANGER ALERT! Array.prototype.remove has already been defined on this browser. '
  errorMessage += 'This may lead to unwanted results when remove() is executed.'
  console.log(errorMessage)
}

删除整数值

var a = [1, 2, 3]
a.remove(2)
a // Output => [1, 3]

删除字符串值

var a = ['a', 'ab', 'abc']
a.remove('abc')
a // Output => ['a', 'ab']

删除布尔值

var a = [true, false, true]
a.remove(false)
a // Output => [true, true]

也可以通过此方法从数组中移除对象Array.prototype.remove方法。只需指定key => value of the Object您想要删除。

删除对象值

var a = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 3, b: 2}]
a.remove({a: 1})
a // Output => [{a: 2, b: 2}, {a: 3, b: 2}]

虽然大多数前一个答复都回答了问题,但现在还不清楚为什么slice()未使用方法。是的,filter()符合不可改变标准,但采用以下较短的等值方法如何?

const myArray = [1,2,3,4];

现在让我们说我们应该从阵列中删除第二个元素, 我们可以简单地做到:

const newArray = myArray.slice(0, 1).concat(myArray.slice(2, 4));

// [1,3,4]

以这种方式从一个阵列中删除元素的方式, 今天社区强烈鼓励从一个阵列中删除元素, 因为它的简单和不可改变的性质。 一般而言, 导致突变的方法应该避免。 例如, 鼓励您替换push()concat()splice()slice().

最干净的:

var arr = ['1','2','3'];
arr = arr.filter(e => e !== '3');
console.warn(arr);

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

您可以使用Set,然后使用delete函数 :

const s = Set;
s.add('hello');
s.add('goodbye');
s.delete('hello');