如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
检查一下这个代码 每个代码都有效主浏览器.
remove_item = function(arr, value) {
var b = '';
for (b in arr) {
if (arr[b] === value) {
arr.splice(b, 1);
break;
}
}
return arr;
};
var array = [1,3,5,6,5,9,5,3,55]
var res = remove_item(array,5);
console.log(res)
其他回答
该员额汇总了截至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来使用。
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)
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);
减少方法的利润如下:
(a) 需要按索引删除某一要素:
function remove(arr, index) {
return arr.reduce((prev, x, i) => prev.concat(i !== index ? [x] : []), []);
}
b) 需要删除元素值(int)的元素:
function remove(arr, value) {
return arr.reduce((prev, x, i) => prev.concat(x !== value ? [x] : []), []);
}
这样我们就可以返回一个新的阵列( 将会以酷酷的功能方式- 比使用推或组合要好得多) , 并删除元素 。
使用 JavaScript 原型特性定义列对象上名为删除() 的方法 。
使用使用复数()满足要求的方法。
请看看下面的代码
Array.prototype.remove = function(item) {
// 'index' will have -1 if 'item' does not exist,
// else it will have the index of the first item found in the array
var index = this.indexOf(item);
if (index > -1) {
// The splice() method is used to add/remove items(s) in the array
this.splice(index, 1);
}
return index;
}
var arr = [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
// Printing array
// [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
console.log(arr)
// Removing 67 (getting its index, i.e. 2)
console.log("Removing 67")
var index = arr.remove(67)
if (index > 0){
console.log("Item 67 found at ", index)
} else {
console.log("Item 67 does not exist in array")
}
// Printing updated array
// [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
console.log(arr)
// ............... Output ................................
// [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ]
// Removing 67
// Item 67 found at 2
// [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ]
注:下面是用完整示例代码执行的完整示例代码。REPL编号js REPL来描述推 ()、 流行 ()、 转移 ()、 非转移 () 和 组合 () 方法的使用 。
> // Defining an array
undefined
> var arr = [12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34];
undefined
> // Getting length of array
undefined
> arr.length;
16
> // Adding 1 more item at the end i.e. pushing an item
undefined
> arr.push(55);
17
> arr
[ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34, 55 ]
> // Popping item from array (i.e. from end)
undefined
> arr.pop()
55
> arr
[ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> // Remove item from beginning
undefined
> arr.shift()
12
> arr
[ 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> // Add item(s) at beginning
undefined
> arr.unshift(67); // Add 67 at beginning of the array and return number of items in updated/new array
16
> arr
[ 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> arr.unshift(11, 22); // Adding 2 more items at the beginning of array
18
> arr
[ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> // Define a method on array (temporarily) to remove an item and return the index of removed item; if it is found else return -1
undefined
> Array.prototype.remove = function(item) {
... var index = this.indexOf(item);
... if (index > -1) {
..... this.splice(index, 1); // splice() method is used to add/remove items in array
..... }
... return index;
... }
[Function]
>
> arr
[ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(45); // Remove 45 (you will get the index of removed item)
3
> arr
[ 11, 22, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(22) // Remove 22
1
> arr
[ 11, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> arr.remove(67) // Remove 67
1
> arr
[ 11, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(89) // Remove 89
2
> arr
[ 11, 67, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(100); // 100 doesn't exist, remove() will return -1
-1
>