如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
从数组中删除一个特定元素, 可以在过滤器选项的一行内完成, 并得到所有浏览器的支持 :https://caniuse.com/#search=filter%20array
function removeValueFromArray(array, value) {
return array.filter(e => e != value)
}
我在这里测试了这个函数 :https://bit.dev/joshk/jotils/remove-value-from-array/~code#test.ts
其他回答
Vanilla JavaScript(ES5.1) - (ES5.1) -已经到位版本版本
浏览器支持 :因特网探索者 9或以后(或以后(详细浏览器支持)
/**
* Removes all occurences of the item from the array.
*
* Modifies the array “in place”, i.e. the array passed as an argument
* is modified as opposed to creating a new array. Also returns the modified
* array for your convenience.
*/
function removeInPlace(array, item) {
var foundIndex, fromIndex;
// Look for the item (the item can have multiple indices)
fromIndex = array.length - 1;
foundIndex = array.lastIndexOf(item, fromIndex);
while (foundIndex !== -1) {
// Remove the item (in place)
array.splice(foundIndex, 1);
// Bookkeeping
fromIndex = foundIndex - 1;
foundIndex = array.lastIndexOf(item, fromIndex);
}
// Return the modified array
return array;
}
Vanilla JavaScript(ES5.1) - (ES5.1) -不可变版本版本
浏览器支持: 与原版的香草 JavaScript 相同
/**
* Removes all occurences of the item from the array.
*
* Returns a new array with all the items of the original array except
* the specified item.
*/
function remove(array, item) {
var arrayCopy;
arrayCopy = array.slice();
return removeInPlace(arrayCopy, item);
}
香草ES6 -不可变版本版本
浏览器支持: Chrome 46, 边缘 12, Firefox 16, Opera 37, Safari 8 ()详细浏览器支持)
/**
* Removes all occurences of the item from the array.
*
* Returns a new array with all the items of the original array except
* the specified item.
*/
function remove(array, item) {
// Copy the array
array = [...array];
// Look for the item (the item can have multiple indices)
let fromIndex = array.length - 1;
let foundIndex = array.lastIndexOf(item, fromIndex);
while (foundIndex !== -1) {
// Remove the item by generating a new array without it
array = [
...array.slice(0, foundIndex),
...array.slice(foundIndex + 1),
];
// Bookkeeping
fromIndex = foundIndex - 1;
foundIndex = array.lastIndexOf(item, fromIndex)
}
// Return the new array
return array;
}
删除在索引i 上的元素, 不突变原始数组 :
/**
* removeElement
* @param {Array} array
* @param {Number} index
*/
function removeElement(array, index) {
return Array.from(array).splice(index, 1);
}
// Another way is
function removeElement(array, index) {
return array.slice(0).splice(index, 1);
}
你的问题没有说明顺序或不同的价值是否是一项要求。
如果您不关心顺序, 并且容器中的值不会超过一次, 请使用“ Set ” 。 它会更快, 更简洁 。
var aSet = new Set();
aSet.add(1);
aSet.add(2);
aSet.add(3);
aSet.delete(2);
更现代一点ECMAScript 2015(原称和谐或ES6)方法。
const items = [1, 2, 3, 4];
const index = 2;
然后:
items.filter((x, i) => i !== index);
弹出 :
[1, 2, 4]
多可惜,您有数组整数,而不是键是这些整数的字符串等量的对象。
我看过很多这样的答案, 在我看来,它们似乎都使用了“粗力 ” ( 粗力 ) 。 我还没有检查每个答案, 如果不是这样的话, 请道歉。 对于一个小的阵列来说,这很好, 但如果你有千个整数呢?
纠正我,如果我错了, 但我们不能假设,在key => value
JavaScript 对象的地图, 即 JavaScript 对象的地图, 关键检索机制可以被假定为高度工程设计和优化吗? (NB:如果一些超级专家告诉我情况并非如此, 我可以建议使用ECMAScript 6's映图类相反,这当然会是) )。
我只是建议,在某些情况下,最好的解决办法可能是 将你的阵列转换成一个物体... 问题当然是,你可能会重复整数值。我建议把这些放在桶中作为“价值”的一部分。key => value
。 (NB: 如果您确定您没有重复的数组元素, 这样可以简单得多 : 值“ 和” 键, 然后直接去Object.values(...)
返回修改后的阵列)。
所以,你可以做到这一点:
const arr = [ 1, 2, 55, 3, 2, 4, 55 ];
const f = function( acc, val, currIndex ){
// We have not seen this value before: make a bucket... NB: although val's typeof is 'number',
// there is seamless equivalence between the object key (always string)
// and this variable val.
! ( val in acc ) ? acc[ val ] = []: 0;
// Drop another array index in the bucket
acc[ val ].push( currIndex );
return acc;
}
const myIntsMapObj = arr.reduce( f, {});
console.log( myIntsMapObj );
产出:
然后很容易删除所有55个数字。
delete myIntsMapObj[ 55 ]; // Again, although keys are strings this works
你不必全部删除: 指数值按外观被挤进桶里, 所以(例如):
myIntsMapObj[ 55 ].shift(); // And
myIntsMapObj[ 55 ].pop();
将分别删除第一次和最后一次发生的情况。 您可以很容易地计算发生频率, 将一个桶的内装物转移到另一个桶等, 将所有55个都替换为3个 。
获取已修改的int
从您的“ bucket 对象” 的数组数组中略微涉及到“ bucket 对象” ,但数量不多:每个桶都包含( 最初数组) 所代表的数值的索引( 以原始数组表示) 。string
关键值 。 这些桶的每个值也是独特的(每个都是独一的)指数指数值原数组中原始数组 : 因此, 您可以在新对象中将其转换为键, 以“ 整数字符串键” 中的( 真实) 整数作为值... 然后排序键然后去Object.values( ... )
.
这听起来很牵扯,很费时... 但显然一切都取决于环境 和理想的用法。我的理解是 JavaScript 的所有版本和背景 只能用一条线运作, 这条线不会“放手 ” , 所以可能会有一些可怕的堵塞 与“强力 ” 方法: 原因不在于它。indexOf
选项,但多次重复slice
/splice
行动。
Addendum如果你是确定这对你的使用来说太过于工程化了 当然最简单的"强力"方法就是
const arr = [ 1, 2, 3, 66, 8, 2, 3, 2 ];
const newArray = arr.filter( number => number !== 3 );
console.log( newArray )
(是的,其他答案已发现)Array.prototype.filter
...)