var arr1 = [5,9,8,4,3,1];//字面量创建数组 var arr2 = newArray(5,9,8,4,3,1); console.log(arr1);//输出:5,9,8,4,3,1 console.log(arr2);//输出:5,9,8,4,3,1 var arr3 = newArray(10); console.log(arr3);//输出:,,,,,,,,,
var arr = ["孙悟空", "猪八戒", "唐僧", "沙和尚"]; var result = arr.pop(); console.log(result); //输出:沙和尚 result = arr.push("白骨精", "玉帝", "蝎子精"); console.log(result); //输出:6 console.log(arr);//输出:孙悟空,猪八戒,唐僧,白骨精,玉帝,蝎子精 result = arr.shift(); console.log(result);//输出:孙悟空 result = arr.unshift("牛魔王", "红孩儿", "铁扇公主"); console.log(result);//输出:8 console.log(arr);//输出:牛魔王,红孩儿,铁扇公主,猪八戒,唐僧,白骨精,玉帝,蝎子精
B.slice splice
1 2 3 4 5 6 7 8 9
var arr = ["孙悟空", "猪八戒", "唐僧", "沙和尚"]; var result = arr.slice(-3,-1); console.log(result);//输出:猪八戒,唐僧 result = arr.slice(-3); console.log(result);//输出:猪八戒,唐僧,沙和尚
result = arr.splice(1,0,"玉帝","佛祖"); console.log(result);//输出:"" console.log(arr);//输出:孙悟空,玉帝,佛祖,猪八戒,唐僧,沙和尚
var arr1 = ["孙悟空","猪八戒","沙和尚"]; var arr2 = ["白骨精","高玉兰","玉兔精"]; var result = arr1.concat(arr2,"牛魔王","红孩儿"); console.log(result);//输出:"孙悟空","猪八戒","沙和尚","白骨精","高玉兰","玉兔精","牛魔王","红孩儿"
var str = result.join(); console.log(str);//输出:"孙悟空,猪八戒,沙和尚,白骨精,高玉兰,玉兔精,牛魔王,红孩儿" str = result.join("");//分隔符为"" console.log(str);//输出:"孙悟空猪八戒沙和尚白骨精高玉兰玉兔精牛魔王红孩儿"