JavaScript How to remove certain element from Array

摘要: In JavaScript, we can combine indexOf() and splice() to remove a certain element from an Array.

In JavaScript, we can combine indexOf() and splice() to remove a certain element from an Array.

        var array = [1, 2, 3, 4, 5];
        console.log(array)
        // I want remove num 4, find the index first, -1 if it is not present.
        var index = array.indexOf(4);
        if (index > -1) { // found
            array.splice(index, 1);
        // array = [1, 2, 3, 5];
        console.log(array);
        var lang = ['java', 'node js', 'javascript', 'pyhton'];
        console.log(lang)
        // I want remove node js
        var index = lang.indexOf(`node js`);
        if (index > -1) {
            lang.splice(index, 1);
        //lang = ['java', 'javascript', 'pyhton'];
        console.log(lang);

上一篇: log4j2.xml example
下一篇: Log4j 2 java.lang.NoClassDefFoundError: com/lmax/disruptor/EventTranslatorVararg
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号