Javascript 如何从数组中删除特定项?
我有一个数字数组,我正在使用Javascript 如何从数组中删除特定项?,javascript,arrays,Javascript,Arrays,我有一个数字数组,我正在使用.push()方法向其中添加元素 有没有一种简单的方法可以从数组中删除特定的元素 我在寻找类似于: array.remove(数字); 我必须使用核心JavaScript。不允许使用框架。Array.prototype.remove\u by\u value=function(val){ for(var i=0;i-1){ 阵列拼接(索引1); } //数组=[2,9] console.log(数组)这取决于您是否想保留一个空白点 如果您确实想要一个空插槽: arr
.push()
方法向其中添加元素
有没有一种简单的方法可以从数组中删除特定的元素
我在寻找类似于:
array.remove(数字);
我必须使用核心JavaScript。不允许使用框架。Array.prototype.remove\u by\u value=function(val){
for(var i=0;i
Array.prototype.remove_by_value=函数(val){
for(var i=0;i
Array.prototype.remove_by_value=function(val){
for(var i=0;i
Array.prototype.remove_by_value=函数(val){
for(var i=0;i console.log(rooms)
我不知道您对数组的期望是什么。有三种可能性我可以想到,你可能会想要
要删除索引i
处的数组元素,请执行以下操作:
阵列拼接(i,1);
如果要从数组中删除每个值为number
的元素:
for(var i=array.length-1;i>=0;i--){
if(数组[i]==编号){
阵列拼接(i,1);
}
}
如果只想使索引i
处的元素不再存在,但不想更改其他元素的索引:
delete array[i];
我不知道您期望数组.remove(int)的行为如何。有三种可能性我可以想到,你可能会想要 要删除索引
i
处的数组元素,请执行以下操作:
阵列拼接(i,1);
如果要从数组中删除每个值为number
的元素:
for(var i=array.length-1;i>=0;i--){
if(数组[i]==编号){
阵列拼接(i,1);
}
}
如果只想使索引i
处的元素不再存在,但不想更改其他元素的索引:
delete array[i];
找到要使用删除的数组元素的
索引
,然后使用删除该索引
splice()方法通过删除
现有元素和/或添加新元素
const数组=[2,5,9];
console.log(数组);
const index=array.indexOf(5);
如果(索引>-1){
阵列拼接(索引1);
}
//数组=[2,9]
console.log(数组)
找到要使用删除的数组元素的索引,然后使用删除该索引
splice()方法通过删除
现有元素和/或添加新元素
const数组=[2,5,9];
console.log(数组);
const index=array.indexOf(5);
如果(索引>-1){
阵列拼接(索引1);
}
//数组=[2,9]
console.log(数组)代码>这取决于您是否想保留一个空白点
如果您确实想要一个空插槽:
array[index] = undefined;
//To keep the original:
//oldArray = [...array];
//This modifies the array.
array.splice(index, 1);
如果您不想要一个空插槽,请执行以下操作:
array[index] = undefined;
//To keep the original:
//oldArray = [...array];
//This modifies the array.
array.splice(index, 1);
如果需要该项的值,只需存储返回的数组元素:
var value = array.splice(index, 1)[0];
如果要在数组的任意一端删除,可以对最后一个使用array.pop()
,对第一个使用array.shift()
(两者都返回该项的值)
如果不知道项目的索引,可以使用array.indexOf(item)
获取它(在If()
中获取一个项目,或在while()中获取所有项目)array.indexOf(item)
返回索引,如果未找到,则返回-1
。这取决于是否要保留一个空位
如果您确实想要一个空插槽:
array[index] = undefined;
//To keep the original:
//oldArray = [...array];
//This modifies the array.
array.splice(index, 1);
如果您不想要一个空插槽,请执行以下操作:
array[index] = undefined;
//To keep the original:
//oldArray = [...array];
//This modifies the array.
array.splice(index, 1);
如果需要该项的值,只需存储返回的数组元素:
var value = array.splice(index, 1)[0];
如果要在数组的任意一端删除,可以对最后一个使用array.pop()
,对第一个使用array.shift()
(两者都返回该项的值)
如果不知道项目的索引,可以使用array.indexOf(item)
获取它(在If()
中获取一个项目,或在while()中获取所有项目)array.indexOf(item)
返回索引或-1
(如果未找到)。如果要删除删除位置的新数组,可以始终删除特定元素并过滤掉数组。对于未实现过滤方法的浏览器,它可能需要扩展,但从长远来看更容易,因为您只需执行以下操作:
var my_array = [1, 2, 3, 4, 5, 6];
delete my_array[4];
console.log(my_array.filter(function(a){return typeof a !== 'undefined';}));
它应该显示[1,2,3,4,6]
如果您想要删除删除位置的新数组,则始终可以删除特定元素并过滤掉该数组。对于未实现过滤方法的浏览器,它可能需要扩展,但从长远来看更容易,因为您只需执行以下操作:
var my_array = [1, 2, 3, 4, 5, 6];
delete my_array[4];
console.log(my_array.filter(function(a){return typeof a !== 'undefined';}));
它应该显示[1,2,3,4,6]
有两种主要方法:
splice():anArray.splice(索引,1)代码>
删除:删除数组[索引]代码>
对数组使用delete
时要小心。它适用于删除对象的属性,但不适用于
function splice(arr, val) {
for (var i = arr.length; i--;) {
if (arr[i] === val) {
arr.splice(i, 1);
}
}
}
Remove all occurrences:
move 0.0048 ms
indexof 0.0463 ms
splice 0.0359 ms
Remove first occurrence:
move_one 0.0041 ms
indexof_one 0.0021 ms
var value = 3
var arr = [1, 2, 3, 4, 5, 3]
arr = arr.filter(function(item) {
return item !== value
})
console.log(arr)
// [ 1, 2, 4, 5 ]
let value = 3
let arr = [1, 2, 3, 4, 5, 3]
arr = arr.filter(item => item !== value)
console.log(arr)
// [ 1, 2, 4, 5 ]
let forDeletion = [2, 3, 5]
let arr = [1, 2, 3, 4, 5, 3]
arr = arr.filter(item => !forDeletion.includes(item))
// !!! Read below about array.includes(...) support !!!
console.log(arr)
// [ 1, 4 ]
// array-lib.js
export function remove(...forDeletion) {
return this.filter(item => !forDeletion.includes(item))
}
// main.js
import { remove } from './array-lib.js'
let arr = [1, 2, 3, 4, 5, 3]
// :: This-Binding Syntax Proposal
// using "remove" function as "virtual method"
// without extending Array.prototype
arr = arr::remove(2, 3, 5)
console.log(arr)
// [ 1, 4 ]
function removeFromArray(array, item, index) {
while((index = array.indexOf(item)) > -1) {
array.splice(index, 1);
}
}
//Set-up some dummy data
var dummyObj = {name:"meow"};
var dummyArray = [dummyObj, "item1", "item1", "item2"];
//Remove the dummy data
removeFromArray(dummyArray, dummyObj);
removeFromArray(dummyArray, "item2");
var removed = helper.removeOne(arr, row => row.id === 5 );
var removed = helper.remove(arr, row => row.name.startsWith('BMW'));
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]
this.array = this.array.filter(function(element, i) {
return element.id !== idToRemove;
});
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']
const items = [1, 2, 3, 4];
const index = 2;
items.filter((x, i) => i !== index);
[1, 2, 4]
var array=['1','2','3','4','5','6']
var newArray = array.filter((value)=>value!='3');
console.log(newArray);
["1", "2", "4", "5", "6"]
var num = [1, 2, 3, 4, 5];
num.splice(num.indexOf(4), 1); // num will be [1, 2, 3, 5];
Array.prototype.remove = Array.prototype.remove || function(x) {
const i = this.indexOf(x);
if(i===-1)
return;
this.splice(i, 1); // num.remove(5) === [1, 2, 3];
}
var num = [5, 6, 5, 4, 5, 1, 5];
const _removeValue = (arr, x) => arr.filter(n => n!==x);
//_removeValue([1, 2, 3, 4, 5, 5, 6, 5], 5) // Return [1, 2, 3, 4, 6]
var myArray = [1, 2, 3, 4, 5, 6];
myArray = myArray.filter(value => value !== 5);
[1, 2, 3, 4, 6]; // 5 has been removed from this array
let arr = [1,2,3,4];
arr.splice(2, 1); //=> arr became [1,2,4]
arr = arr.filter(e => e !== 3); //=> arr became [1,2,4]
theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1);
if (theArray.indexOf("stringToRemoveFromArray") >= 0){
theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1);
}
var array=['1','2','3','4','5','6']
var newArray = array.filter((value)=>value!='3');
var array = ['1','2','3','4','5','6'];
var newArray = array.filter(function(item){ return item !== '3' });