如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
这是我的简单的代码, 用来使用 复数法删除数组中的具体数据。 复数法将给两个参数。 第一个参数是起始数, 第二个参数是删除Count。 第二个参数用于删除从第一个参数值开始的某个元素 。
let arr = [1, 3, 5, 6, 9];
arr.splice(0, 2);
console.log(arr);
其他回答
使用过滤器是实现此要求的优雅方式。 过滤器不会改变原始数组 。
const num = 3; 允许 rr = [1, 2, 3, 4]; const arr2 = arr. filter(x xx x x !x != num); 控制台. log (rr); / / [1, 2, 3, 4] 控制台. log(rr2) / / [1, 2, 4]
如果您想要实现突变删除行为, 您可以使用过滤器, 然后将结果指定到原始数组 。
constnum = 3; let arr = [1, 2, 3, 4]; arr = arr. filter(x x \ x = = = = = = = = = = = = = = = = = =; 控制台. log(rr); / / [1, 2, 4]
顺便说一下, 过滤器将删除在条件中匹配的所有事件( 不仅仅是第一次发生) , 就像您在下面的例子中看到的一样 。
const num = 3; 允许 rr = [1, 2, 3, 3, 3, 3, 4]; arr = arr. filter(x x x x x = = = = = = = = num; 控制台. log (rr); / / [1, 2, 4]
万一您只想删除第一种情况,您可以使用复数法
onst num = 3; 让 arr = [1, 2, 3, 3, 3, 3, 4]; const idx = rr. indexof( num); arr.spice( idx, idx) = = 1 = 1 = 1 = 1 ; 控制台. log( rr) = [ 1, 2, 3, 3, 3, 4] / [ 1, 3, 3, 4]
我认为许多 JavaScript 指令对于功能性编程没有很好的想法。 Splice 返回被删除的元素, 大部分时间您需要减少的数组。 这是不好的 。
想象一下您正在做一个循环调用, 并且不得不通过一个阵列, 并用一个更少的项目, 可能没有当前索引化的项目 。 或者想象一下您正在做另一个循环调用, 并且不得不通过一个带有元素推动的阵列 。
在这两种情况中,你都不能做我的递归功能(myArr.push(c))或我的递归功能(myArr.spice(i,1) ) 。 第一个白痴实际上会通过阵列的长度,第二个白痴会通过删除的元素作为参数。
所以事实上我所做的是... :删除一个阵列元素, 并将结果传递到一个参数的函数中, 同时我做如下:
myRecursiveFunction(myArr.slice(0,i).concat(a.slice(i+1)))
说到推,那更傻...
myRecursiveFunction((myArr.push(c),myArr))
我相信一种正确的功能语言 一种方法突变它所呼吁的物体 就必须返回一个引用 作为结果的物体本身。
更新: 只有当您无法使用 ECMAScript 2015 (前称ES6) 时, 才会推荐使用此方法。 如果您可以使用此方法, 其它答案可以提供更清晰的落实 。
此条格将解决您的问题, 并删除所有参数, 而不是仅一个( 或指定值 ) 。
Array.prototype.destroy = function(obj){
// Return null if no objects were found and removed
var destroyed = null;
for(var i = 0; i < this.length; i++){
// Use while-loop to find adjacent equal objects
while(this[i] === obj){
// Remove this[i] and store it within destroyed
destroyed = this.splice(i, 1)[0];
}
}
return destroyed;
}
用法 :
var x = [1, 2, 3, 3, true, false, undefined, false];
x.destroy(3); // => 3
x.destroy(false); // => false
x; // => [1, 2, true, undefined]
x.destroy(true); // => true
x.destroy(undefined); // => undefined
x; // => [1, 2]
x.destroy(3); // => null
x; // => [1, 2]
我刚刚通过对象.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}]
使用 jQuery 的阵列 :
A = [1, 2, 3, 4, 5, 6];
A.splice($.inArray(3, A), 1);
//It will return A=[1, 2, 4, 5, 6]`
注意: 如果元素未找到, 在阵列中返回 - 1 。