//数组的方法 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"]