如何从数组中删除一个特定值? 类似 :

array.remove(value);

我必须使用核心核心核心JavaScript。 不允许框架 。


当前回答

通过传递其值来删除项目 --

const remove=(value)=>{
    myArray = myArray.filter(element=>element !=value);

}

将项目通过索引编号删除 -

  const removeFrom=(index)=>{
    myArray = myArray.filter((_, i)=>{
        return i!==index
    })
}

其他回答

[2,3,5].filter(i => ![5].includes(i))

除了所有这些解决方案之外, 它也可以用阵列来完成. 减量...

const removeItem = 
    idx => 
    arr => 
    arr.reduce((acc, a, i) =>  idx === i ? acc : acc.concat(a), [])

const array = [1, 2, 3]
const index = 1

const newArray = removeItem(index)(array) 

console.log(newArray) // logs the following array to the console : [1, 3]

...或者一个循环函数(诚实地说不是那么优雅...也许有人有更好的循环解决方案? ? )...

const removeItemPrep = 
    acc => 
    i => 
    idx => 
    arr => 

    // If the index equals i, just feed in the unchanged accumulator(acc) else...
    i === idx ? removeItemPrep(acc)(i + 1)(idx)(arr) :

    // If the array length + 1 of the accumulator is smaller than the array length of the original array concatenate the array element at index i else... 
    acc.length + 1 < arr.length ? removeItemPrep(acc.concat(arr[i]))(i + 1)(idx)(arr) : 

    // return the accumulator
    acc 

const removeItem = removeItemPrep([])(0)

const array = [1, 2, 3]
const index = 1

const newArray = removeItem(index)(array) 

console.log(newArray) // logs the following array to the console : [1, 3]

多可惜,您有数组整数,而不是键是这些整数的字符串等量的对象。

我看过很多这样的答案, 在我看来,它们似乎都使用了“粗力 ” ( 粗力 ) 。 我还没有检查每个答案, 如果不是这样的话, 请道歉。 对于一个小的阵列来说,这很好, 但如果你有千个整数呢?

纠正我,如果我错了, 但我们不能假设,在key => valueJavaScript 对象的地图, 即 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 );

产出:

对象 [ < 1 空位, 数组]1, 阵列[2], 阵列1, 矩阵, 阵列1,<5个空位,46个以上...]

然后很容易删除所有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...)

只删除第一个34年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄34:

ages.splice(ages.indexOf(34), 1);

或者您可以在全球定义一种方法 :

function remove(array, item){
    let ind = array.indexOf(item);
    if(ind !== -1)
        array.splice(ind, 1);
}

将所有年龄都除去34:

ages = ages.filter(a => a !== 34);

使用.indexof () 和.spolice () - 可变模式

这里有两种情况:

  1. 我们知道指数

const drinks = [ 'Tea', 'Coffee', 'Milk'];
const id = 1;
const removedDrink = drinks.splice(id,  1);
console.log(removedDrink)

  1. 我们不知道指数,
    const drinks =  ['Tea','Coffee', 'Milk'];
    const id = drinks.indexOf('Coffee'); // 1
    const removedDrink = drinks.splice(id,  1);
    // ["Coffee"]
    console.log(removedDrink);
    // ["Tea", "Milk"]
    console.log(drinks);

使用. filter () - 不可变模式

你可以想到的最好方式是 — — 而不是“删除”该项目,而是“创造”一个新的阵列,只是不包含该项目。 所以我们必须找到它,完全省略它。

const drinks = ['Tea','Coffee', 'Milk'];
const id = 'Coffee';
const idx = drinks.indexOf(id);
const removedDrink = drinks[idx];
const filteredDrinks = drinks.filter((drink, index) => drink == removedDrink);

console.log("Filtered Drinks Array:"+ filteredDrinks);
console.log("Original Drinks Array:"+ drinks);