如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
我刚刚创建了一个多填充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}]
其他回答
强调.js可用于解决多个浏览器的问题。 如果存在的话, 它会使用在建浏览器的方法。 如果像旧版的互联网探索者一样缺少这些方法, 它会使用自己的自定义方法 。
从数组中删除元素( 从网站) 的简单示例 :
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]
你可以绕过每个array
- 分项目和项目splice
如果它存在于你的体内array
.
function destroy(arr, val) {
for (var i = 0; i < arr.length; i++) if (arr[i] === val) arr.splice(i, 1);
return arr;
}
主要有两种办法:
复数(): anArray.splice(index, 1);
let fruits = ['Apple', 'Banana', 'Mango', 'Orange']
let removed = fruits.splice(2, 1);
// fruits is ['Apple', 'Banana', 'Orange']
// removed is ['Mango']
删除删除: delete anArray[index];
let fruits = ['Apple', 'Banana', 'Mango', 'Orange']
let removed = delete fruits(2);
// fruits is ['Apple', 'Banana', undefined, 'Orange']
// removed is true
使用时要小心delete
用于对数组的数组。它有利于删除对象的属性,但对于数组则不那么好。最好使用splice
用于数组。
请注意,当使用delete
对于一个数组,您可能会获得错误的结果anArray.length
换句话说,delete
将会删除元素, 但它不会更新长度属性的值 。
使用删除后,也可以期望在索引编号上出现空洞,例如,最后可能会有第1、3、4、8、9和11号指数,而之前的长度与使用删除时相同。for
循环会崩溃, 因为索引不再是相继的 。
被迫使用delete
出于某种原因,你应该使用for each
需要通过数组循环时循环循环。事实上,总是避免使用索引for
如果可能的话,循环。这样代码就会更稳健,更不易遇到指数问题。
const Delete = (id) => {
console.log(id)
var index = Array.map(function(e){
return e.id;
}).indexOf(id);
Array.splice(index,1);
}
function removeSingle(array, element) {
const index = array.indexOf(element)
if (index >= 0) {
array.splice(index, 1)
}
}
这对于确保算法在O(N)时间运行更为复杂。
function removeAll(array, element) {
let newLength = 0
for (const elem of array) {
if (elem !== number) {
array[newLength++] = elem
}
}
array.length = newLength
}
array.filter(elem => elem !== number)