如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
您可以用所有存取器示例创建索引 :
<div >
</div>
function getIndex($id){
return (
this.removeIndex($id)
alert("This element was removed")
)
}
function removeIndex(){
const index = $id;
this.accesor.id.splice(index.id) // You can use splice for slice index on
// accessor id and return with message
}
<div>
<fromList>
<ul>
{...this.array.map( accesors => {
<li type="hidden"></li>
<li>{...accesors}</li>
})
}
</ul>
</fromList>
<form id="form" method="post">
<input id="{this.accesors.id}">
<input type="submit" callbackforApplySend...getIndex({this.accesors.id}) name="sendendform" value="removeIndex" >
</form>
</div>
其他回答
let array = [5,5,4,4,2,3,4]
let newArray = array.join(',').replace('5','').split(',')
如果您想要删除一个当前项目, 此示例将有效 。
通过传递其值来删除项目 --
const remove=(value)=>{
myArray = myArray.filter(element=>element !=value);
}
将项目通过索引编号删除 -
const removeFrom=(index)=>{
myArray = myArray.filter((_, i)=>{
return i!==index
})
}
Array.prototype.remove = function(x) {
var y=this.slice(x+1);
var z=[];
for(i=0;i<=x-1;i++) {
z[z.length] = this[i];
}
for(i=0;i<y.length;i++){
z[z.length]=y[i];
}
return z;
}
该员额汇总了截至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来使用。
约翰·里维格( John Resig)已张贴良好执行情况:
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
如果您不想扩展一个全球对象, 可以做一些类似的东西,
// Array Remove - By John Resig (MIT Licensed)
Array.remove = function(array, from, to) {
var rest = array.slice((to || from) + 1 || array.length);
array.length = from < 0 ? array.length + from : from;
return array.push.apply(array, rest);
};
但我之所以张贴这篇文章,主要是为了提醒用户不要采取该页(2007年12月14日)评论中建议的其他实施方式:
Array.prototype.remove = function(from, to) {
this.splice(from, (to=[0, from || 1, ++to - from][arguments.length]) < 0 ? this.length + to : to);
return this.length;
};
它一开始似乎效果良好,但经过一个痛苦的过程,我发现它在试图删除数组中第二至最后一个元素时失败了。例如,如果您有一个 10 元素数组,并且试图用这个来删除第九元素:
myArray.remove(8);
我不知道为什么 但我确认约翰的原创执行没有问题