zoukankan      html  css  js  c++  java
  • js数组都有哪些方法

    //数组的方法     toString(),join(),pop(),push(),shift(),unshift(),splice(),concat(),slice() 
            //1.toString()与join()一样都是把数组转成以逗号分割的字符串
            var fruits=['Banana','Orange','Apple'];
            var foods=['rice']
            console.log(fruits.toString()) // Banana,Orange,Apple
            console.log(fruits.join()) // Banana,Orange,Apple
    
    
            //2.pop() 删除数组的最后一项
            var fruits=['Banana','Orange','Apple'];
            var str=fruits.pop(); //str=Apple
            console.log(fruits) //['Banana','Orange']
            //3.push() 往数组最后添加新数据
            var fruits=['Banana','Orange','Apple'];
            var str=fruits.push('kkk') // str=4 返回的数组的长度        
            console.log(fruits) //['Banana','Orange','Apple','kkk'];    
            
            //4.shift() 删除数组最前面的一项
            var fruits = ["Banana", "Orange", "Apple", "Mango"];
            var str=fruits.shift();   //str=Banana
            console.log(fruits) //["Orange", "Apple", "Mango"];    
            //5. unshift() 往数组开头添加新数据
            var fruits = ["Banana", "Orange", "Apple", "Mango"];
            var str=fruits.unshift('ll');   //str=ll
            console.log(fruits) //["ll", "Banana", "Orange", "Apple", "Mango"]
            //6. splice() 方法可用于向数组添加新项或者删除数组
            //新增
            var fruits = ["Banana", "Orange", "Apple", "Mango"];
            fruits.splice(2, 0, "Lemon", "Kiwi");
            console.log( fruits)// ["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]
             //删除
            var fruits = ["Banana", "Orange", "Apple", "Mango"];
            fruits.splice(2, 1);
            console.log(fruits)// ["Banana", "Orange", "Mango"]
            //7.concat()连接两个数组生成一个新数组
            var fruits= ["Banana", "Orange", "Mango"];
            var fruits1=['222'];
            console.log(fruits.concat(fruits1)) //["Banana", "Orange", "Mango", "222"]
            //8.slice()  
            var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
            var citrus = fruits.slice(1); 
            console.log(citrus) // (4) ["Orange", "Lemon", "Apple", "Mango"]
  • 相关阅读:
    MVC中单选按钮的实现
    前端点击手机号码跳转到手机拨号页面
    jQuery限制文本框只能输入正整数
    Asp.Net 之 二维码生成
    MVC +Jqyery+Ajax 实现弹出层提醒
    jQuery Ajax使用实例
    ASP的调试技术解答
    模式应用场景
    redis笔记
    yii resful
  • 原文地址:https://www.cnblogs.com/h5it/p/12745408.html
Copyright © 2011-2022 走看看