如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
您可以使用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']
其他回答
例如,您有一个字符数组,想要从数组中删除“A”。
数组有一个过滤方法,可以过滤并只返回您想要的元素。
let CharacterArray = ['N', 'B', 'A'];
我想返回元素 除了"A"。
CharacterArray = CharacterArray.filter(character => character !== 'A');
字符阵列必须是 : ['N', 'B']
**simple array**
const arr = ['1','2','3'];
const updatedArr = arr.filter(e => e !== '3');
console.warn(updatedArr);
**array of object**
const newArr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}]
const updatedNewArr = newArr.filter(e => e.id !== 3);
console.warn(updatedNewArr);
**array of object with different parameter name**
const newArr = [{SINGLE_MDMC:{id:1,cout:10}},{BULK_MDMC:{id:1,cout:15}},{WPS_MDMC:{id:2,cout:10}},]
const newArray = newArr.filter((item) => !Object.keys(item).includes('SINGLE_MDMC'));
console.log(newArray)
您可以从数组中添加一个原型函数来“ 移除” 元素 。
以下示例显示当我们知道一个元素的索引时如何简单地从数组中删除一个元素。Array.filter
方法。
Array.prototype.removeByIndex = function(i) {
if(!Number.isInteger(i) || i < 0) {
// i must be an integer
return this;
}
return this.filter((f, indx) => indx !== i)
}
var a = [5, -89, (2 * 2), "some string", null, false, undefined, 20, null, 5];
var b = a.removeByIndex(2);
console.log(a);
console.log(b);
有时候我们不知道元素的索引
Array.prototype.remove = function(i) {
return this.filter(f => f !== i)
}
var a = [5, -89, (2 * 2), "some string", null, false, undefined, 20, null, 5];
var b = a.remove(5).remove(null);
console.log(a);
console.log(b);
// It removes all occurrences of searched value
但是,当我们只想要删除搜索值的首次出现时,我们就可以使用Array.indexOf
函数中的方法。
Array.prototype.removeFirst = function(i) {
i = this.indexOf(i);
if(!Number.isInteger(i) || i < 0) {
return this;
}
return this.filter((f, indx) => indx !== i)
}
var a = [5, -89, (2 * 2), "some string", null, false, undefined, 20, null, 5];
var b = a.removeFirst(5).removeFirst(null);
console.log(a);
console.log(b);
我刚刚创建了一个多填充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}]
通常比较好的是 创造一个新的阵列filter
函数。
let array = [1,2,3,4];
array = array.filter(i => i !== 4); // [1,2,3]
这还提高了易读性IMHO。slice
虽然它知道有时你应该去。