如何从数组中删除一个特定值? 类似 :
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}]
其他回答
查找index
使用indexOf
,然后删除该索引splice
.
组合法通过删除现有元素和/或添加新元素来改变数组的内容。
const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
array.splice(index, 1); // 2nd parameter means remove one item only
}
// array = [2, 9]
console.log(array);
第二个参数的第二个参数splice
是要删除的元素数。请注意splice
修改现有数组,并返回含有已删除元素的新数组。
由于完整性的原因,此处为函数。第一个函数只删除一个单一事件(即删除第一个匹配5
调自[2,5,9,1,5,8,5]
),而第二个函数删除所有事件:
function removeItemOnce(arr, value) {
var index = arr.indexOf(value);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
function removeItemAll(arr, value) {
var i = 0;
while (i < arr.length) {
if (arr[i] === value) {
arr.splice(i, 1);
} else {
++i;
}
}
return arr;
}
// Usage
console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
console.log(removeItemAll([2,5,9,1,5,8,5], 5))
在类型Script中,这些函数可用类型参数保持类型安全:
function removeItem<T>(arr: Array<T>, value: T): Array<T> {
const index = arr.indexOf(value);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
Array.prototype.remove = function(x) {
var y=this.slice(x+1);
var z=[];
for(i=0;i<=x-1;i++) {
z[z.length] = this[i];
}
for(i=0;i<y.length;i++){
z[z.length]=y[i];
}
return z;
}
您可以使用lodash_.pull(平衡数组). pullAt(阵数数组)或_. 无 _(不变异阵列)
var array1 = ['a', 'b', 'c', 'd']
_.pull(array1, 'c')
console.log(array1) // ['a', 'b', 'd']
var array2 = ['e', 'f', 'g', 'h']
_.pullAt(array2, 0)
console.log(array2) // ['f', 'g', 'h']
var array3 = ['i', 'j', 'k', 'l']
var newArray = _.without(array3, 'i') // ['j', 'k', 'l']
console.log(array3) // ['i', 'j', 'k', 'l']
使用 jQuery 的阵列 :
A = [1, 2, 3, 4, 5, 6];
A.splice($.inArray(3, A), 1);
//It will return A=[1, 2, 4, 5, 6]`
注意: 如果元素未找到, 在阵列中返回 - 1 。
Array.prototype.remove = function(start, end) {
var n = this.slice((end || start) + 1 || this.length);
return this.length = start < 0 ? this.length + start : start,
this.push.apply(this, n)
}
start
和end
可以是负数。在这种情况下,它们会从数组的末尾计数。
如果只有start
中指定,只删除一个元素。
函数返回新数组长度。
z = [0,1,2,3,4,5,6,7,8,9];
newlength = z.remove(2,6);
(8) [0, 1, 7, 8, 9]
z=[0,1,2,3,4,5,6,7,8,9];
newlength = z.remove(-4,-2);
(7) [0, 1, 2, 3, 4, 5, 9]
z=[0,1,2,3,4,5,6,7,8,9];
newlength = z.remove(3,-2);
(4) [0, 1, 2, 9]