xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js & array remove one item ways


// array remove one item ways

    let keys = [1,2,3,4,5,6,7];
    let key = 3;
    // keys.remove(key); ???
    let index = keys.indexOf(key);
    // keys = keys.splice(index, 1);
    // keys = keys.slice(index, index + 1);
    keys = keys.filter(icon => icon !== key);


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

let animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
 
console.log(animals.slice(4, 5));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice



let months = ['Jan', 'March', 'April', 'June'];
 
// remove one
months.splice(2, 1);
 
console.log(months);
// expected output: Array ['Jan', 'March', 'June']


https://www.cnblogs.com/xgqfrms/p/11039499.html


原文地址:https://www.cnblogs.com/xgqfrms/p/11040792.html