如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
该员额汇总了截至2001年1月1日从数组中删除元素的通用方法ECMAScript 2019(ES10)。
.splice()
| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 是( loop) , 否( indexof)
| 按值/ 指数:按指数:___________________________________________________________________________________________________________________________________________________________________________________
如果您知道要从数组中删除的值, 您可以使用复数法。 首先, 您必须确定目标项的索引。 然后使用索引作为起始元素, 并删除一个元素 。
// With a 'for' loop
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
for( let i = 0; i < arr.length; i++){
if ( arr[i] === 5) {
arr.splice(i, 1);
}
} // => [1, 2, 3, 4, 6, 7, 8, 9, 0]
// With the .indexOf() method
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const i = arr.indexOf(5);
arr.splice(i, 1); // => [1, 2, 3, 4, 6, 7, 8, 9, 0]
.filter()
方法| 在职: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数: 按数值 :%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
具体要素可以是已经过滤从数组中,通过提供过滤功能,从数组中取出。然后对数组中的每个元素要求使用此函数。
const value = 3
let arr = [1, 2, 3, 4, 5, 3]
arr = arr.filter(item => item !== value)
console.log(arr)
// [ 1, 2, 4, 5 ]
Array.prototype
| 在职:是/否(取决于执行情况)__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复:是/否(取决于执行情况)__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:按指数 / 按数值(视执行情况而定)
阵列原型可以通过其他方法扩展,然后在创建的阵列上使用这些方法。
注:一些人认为,从JavaScript标准图书馆(如Array)扩展物体原型是反型。
// In-place, removes all, by value implementation
Array.prototype.remove = function(item) {
for (let i = 0; i < this.length; i++) {
if (this[i] === item) {
this.splice(i, 1);
}
}
}
const arr1 = [1,2,3,1];
arr1.remove(1) // arr1 equals [2,3]
// Non-stationary, removes first, by value implementation
Array.prototype.remove = function(item) {
const arr = this.slice();
for (let i = 0; i < this.length; i++) {
if (arr[i] === item) {
arr.splice(i, 1);
return arr;
}
}
return arr;
}
let arr2 = [1,2,3,1];
arr2 = arr2.remove(1) // arr2 equals [2,3,1]
delete
操作员| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:按指数:___________________________________________________________________________________________________________________________________________________________________________________
使用删除运算符不会影响长度属性。 它也不会影响后续元素的索引。 数组变得稀少, 这是一种巧妙的方式, 表示删除的项目没有被删除, 但却没有定义 。
const arr = [1, 2, 3, 4, 5, 6];
delete arr[4]; // Delete element with index 4
console.log( arr ); // [1, 2, 3, 4, undefined, 6]
删除运算符的设计是为了从 JavaScript 对象中删除属性,而数组则是对象。
Object
公用事业(ES10)| 在职: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数: 按数值 :%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A. 实行ES-1010Object.fromEntries
,可以用来从任何类似阵列的物体中创建所需的阵列,并在过程中过滤不想要的元素。
const object = [1,2,3,4];
const valueToRemove = 3;
const arr = Object.values(Object.fromEntries(
Object.entries(object)
.filter(([ key, val ]) => val !== valueToRemove)
));
console.log(arr); // [1,2,4]
length
| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________
JavaScript 阵列元素可以通过将长度属性设定为低于当前值的值,从数组的末尾删除。任何指数大于或等于新长度的元素将被删除 。
const arr = [1, 2, 3, 4, 5, 6];
arr.length = 5; // Set length to remove element
console.log( arr ); // [1, 2, 3, 4, 5]
.pop()
方法| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________
流行方法删除数组最后一个元素, 返回该元素, 并更新长度属性。 流行方法修改援引该元素的数组, 这意味着与使用删除最后一个元素的方式不同, 将完全删除最后一个元素, 并缩小数组长度 。
const arr = [1, 2, 3, 4, 5, 6];
arr.pop(); // returns 6
console.log( arr ); // [1, 2, 3, 4, 5]
| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________
缩略.shift()
方法与流行法非常相似, 但它删除了 JavaScript 数组的第一个元素, 而不是最后一个元素。 当元素被删除时, 其余元素会被移到下方 。
const arr = [1, 2, 3, 4];
arr.shift(); // returns 1
console.log( arr ); // [2, 3, 4]
| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________
最快的技术是设置空数组的数组变量。
let arr = [1];
arr = []; //empty array
2.1.1的替代技术可通过将长度设定为0来使用。
其他回答
var index,
input = [1,2,3],
indexToRemove = 1;
integers = [];
for (index in input) {
if (input.hasOwnProperty(index)) {
if (index !== indexToRemove) {
integers.push(result);
}
}
}
input = integers;
此解决方案将需要一系列输入, 并将通过输入查找要删除的值。 这将在整个输入数组中循环, 结果将是第二个已经删除了特定索引的数组整数组。 然后将整数组复制到输入数组中 。
let removeAnElement = (arr, element)=>{
let findIndex = -1;
for (let i = 0; i<(arr.length); i++){
if(arr[i] === element){
findIndex = i;
break;
}
}
if(findIndex == -1){
return arr;
}
for (let i = findIndex; i<(arr.length-1); i++){
arr[i] = arr[i+1];
}
arr.length -= 1;
return arr;
}
let array = ['apple', 'ball', 'cat', 'dog', 'egg'];
let removeElement = 'ball';
let tempArr2 = removeAnElement(array, 'dummy');
console.log(tempArr2);
// ['apple', 'cat', 'dog', 'egg']
let tempArr = removeAnElement(array, removeElement);
console.log(tempArr);`enter code here`
// ['apple', 'cat', 'dog', 'egg']
spolice () 函数能够将数组中的项目还给您, 从特定的索引中删除项目 / 项 :
function removeArrayItem(index, array) {
array.splice(index, 1);
return array;
}
let array = [1,2,3,4];
let index = 2;
array = removeArrayItem(index, array);
console.log(array);
只删除第一个34
年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄34
:
ages.splice(ages.indexOf(34), 1);
或者您可以在全球定义一种方法 :
function remove(array, item){
let ind = array.indexOf(item);
if(ind !== -1)
array.splice(ind, 1);
}
将所有年龄都除去34
:
ages = ages.filter(a => a !== 34);
我刚刚创建了一个多填充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}]