如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
由你决定如何行动。
一种办法是使用复数法,从数组中删除项目:
let array = [1, 2, 3]
array.splice(1, 1);
console.log(array)
// return [1, 3]
但要确保您通过第二个参数,否则最终会删除索引后的全部数组。
第二个办法是使用过滤法,其好处在于它不可改变,这意味着你的主阵列不会被操纵:
const array = [1, 2, 3];
const newArray = array.filter(item => item !== 2)
console.log(newArray)
// return [1, 3]
其他回答
用于解决多个浏览器的问题。 如果存在的话, 它会使用在建浏览器的方法。 如果没有这些方法, 则使用自己的自定义方法。 如果像旧版的互联网探索者一样, 它会使用自己的自定义方法 。
从数组中删除元素( 从网站) 的简单示例 :
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]
const Delete = (id) => {
console.log(id)
var index = Array.map(function(e){
return e.id;
}).indexOf(id);
Array.splice(index,1);
}
使用 JavaScript 原型特性定义列对象上名为删除() 的方法 。
使用 spolice () 方法满足您的要求 。
请看看下面的代码
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 ]
注:以下是Norde.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
>
使用 JavaScript 从数组中删除一个项的几种方法 。
所描述的所有方法都不改变原始数组,而是创建一个新的数组。
如果您知道某个项目的索引
假设您有一个阵列, 您想要删除位置 i 上的项目 。
一种方法是使用切片( ) :
const items = [a], 'b','c','d','e','f'] const i = 3 const 过滤项目 = items.selice(0, i).concat( items.selice( i+1, items. long)) 控制台.log( 过滤项目)
切片 () 创建新数组, 使用它收到的索引 。 我们只需创建一个新数组, 从开始到要删除的索引, 并且将另一个数组从我们删除后的第一个位置集中到数组的末尾 。
如果您知道数值
在这种情况下,一个很好的选择是使用过滤器(),这提供了一种更宣示性的方法:
Const 项 = [“ a ”、“ b ” 、 “ c ” 、 “ d ” 、 “ e ” 、 “ f ” 、 “ f ” 等值 Toremove = “ c” 等式过滤项目 = 项. filter (项目 项 ! ! ! = ex 值 to remove) 控制台.log (过滤项目 )
此选项使用 ES6 箭头函数。 您可以使用传统函数支持旧的浏览器 :
const items = ['a', 'b','c','d','d','e','f'] const value to remove ='c' const 过滤项目= items. filter(功能(项目) {返回项目!}== value to remove}控制台.log(过滤项目)
或者你可以使用 Babel 将ES6代码转换回ES5, 使旧浏览器更容易消化, 而在您的代码中写入现代 JavaScript 。
删除多个项目
如果不是单项,而是要删除许多项,会怎么样?
让我们找到最简单的解决方案
按指数分列的指数
您可以在序列中创建函数并删除项目 :
const items = [a], b', c', d', d', e', e', e', f'] const demote =( items, i) = items = (items, i, i' i) {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}} {{{{{{{{{{{{{{{{{{{{}}}}}} {{{{{{{{{{{{{{}}}{{{{{{{{{{{{{{}}}}{{{{{{{{{{{{{{{{{{{}}}} {{{{{{{{{{{{{}}}}}{{{{{{{{}}}}}}}{{{{{{{{}}}}}}{{{{{{{{{{{{}}}}}}}}}}}{{{{{{{{{}}}}}}}}}}}}}}}{{{{{}}}}}}}{{}}}}{{{{{}}}}}{{{{{}}}}}}}}}{{{{{{{{{{{{{{{{}}}}}{{{{{{{{{{}}}}}}}}{{{{}}}}{{{{{{{{{{}}}}{}}}}}}}}}{}}}}}}}}}}}}}}}}}}{}}}}}}}}}}}{{}{{}{}{}}}}{{}}}}}}}}}}{}{}{}
以数值计
您可以在回溯函数中搜索包含内容 :
const items = ['a', 'b', 'c','d','d','e','f'] const value to remove = ['c','d'] comst filtems = items. filter(项目 \\\ ? $values to remove.) 包括(项目) // ["a","b","e","f" 控制台。log(过滤项目)
避免突变原始数组
plice() spolice () (不得与切片() 相混淆) 使原始数组变形, 并应避免 。
(最初张贴在我的网站上:https://flaviocopes.com/how-to-remove-itm-from-array/)
使用$.inAray, 以其价值删除一个元素:
$(document).ready(function(){
var arr = ["C#","Ruby","PHP","C","C++"];
var itemtoRemove = "PHP";
arr.splice($.inArray(itemtoRemove, arr),1);
});