如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
多可惜,您有数组整数,而不是键是这些整数的字符串等量的对象。
我看过很多这样的答案, 在我看来,它们似乎都使用了“粗力 ” ( 粗力 ) 。 我还没有检查每个答案, 如果不是这样的话, 请道歉。 对于一个小的阵列来说,这很好, 但如果你有千个整数呢?
纠正我,如果我错了, 但我们不能假设在关键值地图中, JavaScript 物体的种类就是 JavaScript 对象, 关键检索机制可以被假定为高度工程化和优化? (NB: 如果一些超级专家告诉我情况并非如此, 我建议使用ECMAScript 6 的地图类, 这当然会是这样 ) 。
我只是建议,在某些情况下,最好的解决方案可能是将您的阵列转换为对象... 当然,问题在于您可能有重复的整数值。 我建议将那些放在桶中作为关键 {} {} 值条目中的“ 价值” 部分。 (NB: 如果您确定您没有重复的阵列元素, 这样可以简单得多 : 值“ 和” 键, 只需去对象. 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 );
产出:
对象 [<1个空位>、Array1、Array[2]、Array1、Array1、<5个空位]、46个以上...]
然后很容易删除所有55个数字。
delete myIntsMapObj[ 55 ]; // Again, although keys are strings this works
你不必全部删除: 指数值按外观被挤进桶里, 所以(例如):
myIntsMapObj[ 55 ].shift(); // And
myIntsMapObj[ 55 ].pop();
将分别删除第一次和最后一次发生的情况。 您可以很容易地计算发生频率, 将一个桶的内装物转移到另一个桶等, 将所有55个都替换为3个 。
从您的“ bucket 对象” 中获取修改的整数组略为涉及, 但数量不多 : 每个桶都包含以( 字符串) 键表示的值的索引( 原始数组) 。 这些桶的每一个值也是独一无二的( 每个是原始数组中唯一的索引值 ) : 所以您把它们转换成新对象中的键, 将“ 整数” 中的( 真实) 整数作为值... 然后对键进行排序, 然后对对象. values (...) 排序 。
这听起来很牵强,很费时... 但显然一切都取决于环境 和理想的用法。我的理解是 JavaScript 的所有版本和背景 只能用一条线运作, 这条线不会“放手 ” , 所以可能会有一些可怕的拥堵 , 使用“强力” 方法: 不是由 Enterof 行动造成的, 而是由多个重复的切片/碎块操作造成的。
增编 如果你确定这对你来说太过分工程了 使用这个案子的话 最简单的"强力"方法肯定就是
const arr = [ 1, 2, 3, 66, 8, 2, 3, 2 ];
const newArray = arr.filter( number => number !== 3 );
console.log( newArray )
(是的,其他的答案 已经发现阵列. 原型. 过滤器... )
其他回答
ES6且无突变:(2016年10月)
const removeByIndex = (list, index) => [ ...list.slice(0, index), ...list.slice(index + 1) ]; output = removeByIndex([33,22,11,44],1) //=> [33,11,44] console.log(output)
您可以使用 JavaScript 的标准 __ proto__ 来定义此函数。例如,
让数据 = [;] 数据 .__proto__.remove = (n) { {数据 = 数据 : blockMap ((v) ) { { 返回 v ? v : [] n? v ; 数据 = [1, 2, 3] ; 数据 = [1, 2, 3] ; 数据.remove (2); 控制台.log(数据); / / [1,3] 数据 = [a', b', c'] ; 数据.remove ('b'); 控制台.log(数据); // [a, c]
不需要使用 indexof 或 spolice 。 但是, 如果您只想要删除一个元素的发生, 它的效果会更好 。
查找并移动( 移动) :
function move(arr, val) {
var j = 0;
for (var i = 0, l = arr.length; i < l; i++) {
if (arr[i] !== val) {
arr[j++] = arr[i];
}
}
arr.length = j;
}
使用索引和串点( 索引) :
function indexof(arr, val) {
var i;
while ((i = arr.indexOf(val)) != -1) {
arr.splice(i, 1);
}
}
只使用复数( 复数) :
function splice(arr, val) {
for (var i = arr.length; i--;) {
if (arr[i] === val) {
arr.splice(i, 1);
}
}
}
带有 1000 元素的阵列( 平均超过 10,000 次运行) 的节点js 上的运行时间 :
指数比移动要慢10倍左右。 即使通过删除对 Enterof 的调用来改进指数,它的表现也比移动差得多。
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
Array. prototype. removeByValue = 函数( val) {, 对于 (var i = 0; i < this. long; i++) { 如果 (i) val) { this. splice (i, 1); i- ; } 返回此 ;} var falts = [apple, 'banana', 'carrot', 'orange' ] ; falves.remove ByValue ('banana'); 礼服. log (fruts); / / - > [apple, 'carrot', 'orange' ]
Vanilla JavaScript(ES5.1) - 已建立版本
浏览器支持: Internet Explorer 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) - 永恒版本
浏览器支持: 与原版的香草 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);
}
Vanilla 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;
}