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

js & array remove one item ways

js & array remove one item ways

splice

https://stackoverflow.com/a/55686164/5934465

array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

splice() 方法通过删除替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。
此方法会改变原数组。

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/splice


// 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']

refs

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



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2019-06-17 17:06  xgqfrms  阅读(174)  评论(1编辑  收藏  举报