js数组知识总结

js中的数组的创建和遍历是最基本的;此处不需要笔记。

1.合并两个数组arr1.concat(arr2);

eg:

var arr = new Array(3)

arr[0] = "George"

arr[1] = "John"

arr[2] = "Martin"

var arr2 = new Array(3)

arr2[0] = "James"

arr2[1] = "Adrew"

arr2[2] = "Martin"

document.write(arr.concat(arr2))

2.数组转成字符串 arr.join(",");以“,”为分隔符转化成字符串。

3.文字数组的排序:arr.sort();从字面上对数组中的字符串进行排序。

4.数字数组的排序:arr.sort();从数值上对数组进行从小到大排序。

eg:

function sortNumber(a, b)

{return a - b}

var arr = new Array(6)

arr[0] = "10";arr[1] = "5";arr[2] = "40";arr[3] = "25";arr[4] = "1000";arr[5] = "1";

document.write(arr.sort(sortNumber))

你可能感兴趣的:(js数组知识总结)