数组处理方法
//定义数组var array = [];undefined//查看类型typeof(array);"object"//往数组里添加数据array = ['first','second','third']["first", "second", "third"]//数组长度array.length3//索引array[0]"first"//添加数组新内容array[3] = 'fourth'"fourth"array["first", "second", "third", "fourth"]//使用push添加数组的新数据,是往数组后添加数据array.push('fifth','sixth')6array["first", "second", "third", "fourth", "fifth", "sixth"]//往前添加是使用unshift-------------------var newArray = ["second", "three", "four", "one", "two"]newArray.unshift('haha')6newArray["haha", "second", "three", "four", "one", "two"]//-----------------------//删除数据最后一个元素使用poparray.pop()"sixth"array["first", "second", "third", "fourth", "fifth"]///删除数组第一个元素使用shiftarray.shift()"first"///删除数组中某一个元素的值会使用delete,不会删除数组中该元素delete array[3]truearray["second", "third", "fourth", undefined × 1]//彻底删除数组里的元素使用splice方法array.splice(3)[undefined × 1]array["second", "third", "fourth"]//合并两个数组使用concatvar array2 = ['one','two']undefinedvar newArray = array.concat(array2)undefinednewArray["second", "third", "fourth", "one", "two"]
本文详细介绍了JavaScript中数组的各种操作方法,包括定义数组、查看数组类型、添加和删除数组元素等基本操作,同时还介绍了如何合并数组及使用一些高级方法如splice进行数组元素的精确控制。
5977

被折叠的 条评论
为什么被折叠?



