如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
这提供了一个前提值, 而不是一个值 。
注:它将更新给定阵列,并返回受影响的行。
用法
var removed = helper.remove(arr, row => row.id === 5 );
var removed = helper.removeAll(arr, row => row.name.startsWith('BMW'));
定义定义定义定义定义的定义定义定义定义定义的定义定义定义定义定义的定义定义定义定义定义定义的定义
var helper = {
// Remove and return the first occurrence
remove: function(array, predicate) {
for (var i = 0; i < array.length; i++) {
if (predicate(array[i])) {
return array.splice(i, 1);
}
}
},
// Remove and return all occurrences
removeAll: function(array, predicate) {
var removed = [];
for (var i = 0; i < array.length; ) {
if (predicate(array[i])) {
removed.push(array.splice(i, 1));
continue;
}
i++;
}
return removed;
},
};
其他回答
我刚刚通过对象.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}]
删除上次的元素
arrName.pop();
删除第一个元素
arrName.shift();
从中间删除
arrName.splice(starting index, number of element you wnt to delete);
Example: arrName.splice(1, 1);
删除最后一个元素
arrName.splice(-1);
使用数组索引编号删除
delete arrName[1];
更现代的ECMAScript 2015(原称和谐或ES6)方法。
const items = [1, 2, 3, 4];
const index = 2;
然后:
items.filter((x, i) => i !== index);
弹出 :
[1, 2, 4]
您可以使用 Babel 和多填充服务,以确保浏览器之间有很好的支持。
您只需要按元素或索引过滤 :
var num = [5, 6, 5, 4, 5, 1, 5]; var result1 = num.filter((el, index) => el != 5) // for remove all 5 var result2 = num.filter((el, index) => index != 5) // for remove item with index == 5 console.log(result1); console.log(result2);
我建议删除一个使用删除和过滤的阵列项目:
var arr = [1,2,3,4,5,5,5,6,7,8,9];删除ar[5];rr = arr.filter(职能(项目){返回项目!=未定义;});//结果:[1、2,3,4,5,6,6,7,7,8,9];删除ar[5];ar = arr.filter(职能(项目){返回项目);//结果:[1、2,3,4,6,6,6,7,7,8,9] 控制台。
因此,我们只能删除一个特定的数组项目,而不是所有具有相同价值的项目。