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

array.remove(value);

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


当前回答

由你决定如何行动。

一种办法是使用复数法,从数组中删除项目:

let array = [1, 2, 3]
array.splice(1, 1);
console.log(array)

// return [1, 3]

但要确保您通过第二个参数,否则最终会删除索引后的全部数组。

第二个办法是使用过滤法,其好处在于它不可改变,这意味着你的主阵列不会被操纵:

const array = [1, 2, 3];
const newArray = array.filter(item => item !== 2)
console.log(newArray)

// return [1, 3]

其他回答

从字符串阵列中查找和删除一个特定的字符串:

var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car"); // Get "car" index
// Remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red", "blue", "green"]

资料来源:https://www.codegrepper.com/?search_term=remove+a+particulate+element+ from+array? 来源:https://www.codegrepper.com/?search_term=remove+a+partical+element+ from+array

我刚刚通过对象.defineProperty在 Array. prototype 上创建了一个多填充器, 以便删除一个阵列中想要的元素, 而不会导致在... 的... 。

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 方法从数组中移除对象。您只需要指定要删除对象的键值 {% 。

删除对象值

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}]

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

类似的东西 :

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

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

您在数组中有 1 到 9 个, 您想要删除 5 个 。 请使用以下代码 :

var number Array = [1、2、3、4、5、6、7、8、9]; var new numberArray = number Array. filter (m { return m!= 5;}); 控制台.log ("新阵列,5 develop",新的数字阵列);


如果您想要多个值。例如:- 1,7,8

var number Array = [1、2、3、4、5、6、7、8、9]; var new numberArray = number Array. filter (m {{{{ {返回} (m = = == 1) {(m = = = 7) {(m = = 7) { (m = = = = = 7;} 控制台.log ("新阵列,1,7和8 移除",新的数字阵列);


如果您想要删除数组中的数组值。 例如 : [3,4,5]

var number Array = [1、2、3、4、5、6、7、8、9]; var remoble Array = [3、4、5、5、6、7、8、9]; var new number Array = number Array. filter (m { { return $remobleAry. includs(m) } 控制台.log ("新阵列, [3、4、5] 移除" , 新的数字阵列);

包括支持的浏览器为链接 。

您可以使用ES6. 例如,在此情况下删除值“ 3” :

var array=['1','2','3','4','5','6']
var newArray = array.filter((value)=>value!='3');
console.log(newArray);

产出:

["1", "2", "4", "5", "6"]