//【檢測陣列】
//1.對於乙個網頁,或者乙個全域性作用域而言,使用instaceof操作符
if(value instanceof array)
//2.如果網頁中包含多個框架,那實際上存在2個以上不同的全域性執行環境
//ecmascipt5新增了 array.isarray()方法,最終確定某個值到底是不是陣列,而不管它在哪個全域性執行環境中建立的
if(array.isarray(value))
//支援array.isarray()方法的瀏覽器有ie9+、firefox 4+、safari 5+、opera 10.5+ 和chrome
//3.在尚未實現array.isarray()方法的瀏覽器中準確的檢測陣列
function isarray(value)
//【轉換方法】
//tolocalestring()、tostring() 和 valueof()
//1.呼叫陣列的 tostring()方法會返回由陣列中的每個值的字串形式拼接而成的乙個以逗號分隔的字元
//而呼叫valueof()返回的還是陣列
var colors = ["a","b","c"];
alert(colors.tostring()); //a,b,c
alert(colors.valueof()); //a,b,c
alert(colors); //a,b,c
//說明:由於alert()要接收字串引數,所以它會在後台呼叫tostring()方法,由此會得到與直接呼叫tostring()方法相同的結果
//2.使用join()方法,可以使用不同的分隔符來構建這個字串
var colors2 = ["aa","bb","cc"];
alert(colors2.join(",")); //aa,bb,cc
alert(colors2.join("-")) //aa-bb-cc
//【棧方法】
//棧是一種後進先出的資料結構,也就是最新新增的項最早被移除。而棧中項的插入和移除,只發生在棧的頂部。
方法可以接受任意數量的引數,把它們逐個新增到陣列的末尾,並返回陣列修改後的長度
var colors = new array(); //建立乙個陣列
var count = colors.push("red","green"); //推入2項
alert(count); //2
count = colors.push("black"); //推入另1項
alert(count); //3
colors[3] = "blue";
alert(colors.length); //4
方法,彈出陣列末尾的一項
var item = colors.pop();
alert(item); //"blue"
//【佇列方法】
//佇列資料結構的訪問規則是先進先出,在列表末端新增項,從列表前端移除項
移除陣列中第一項並返回該項,同時將長度減1
//結合使用shift()和push()方法,可以像使用佇列一樣使用陣列
var colors = new array();
var count = colors.push("red","green"); //推入2項
alert(count); //2
count = colors.push("black"); //推入另一項
alert(count); //3
var item = colors.shift();
alert(item); //"red"
alert(colors.length); //2
在陣列前端新增任意個項,並返回陣列的長度
var colors = new array();
colors.unshift("red","green");
alert(colors); //"red","green"
count = colors.unshift("black");
alert(count); //3
var item = colors.pop();
alert(item); //"black"
//【重排序方法】
//1.反轉陣列的順序
var values = [1,2,3,4,5];
values.reverse();
alert(values); //5,4,3,2,1
方法按公升序排列陣列項——即最小的值位於最前面,最大的值在最後面
var values2 = [0,1,5,10,15];
values2.sort();
alert(values2); //0,1,10,15,5
/*sort()方法會呼叫每個陣列元素的tostring()轉型方法,然後比較得到的字串
這種排序方式在很多情況下都不是最佳方案*/
方法可以接收乙個比較函式作為引數
function sort_asc(v1,v2)else if(v1 > v2)else }
values2.sort(sort_asc);
alert(values2); //0,1,5,10,15
function sort_desc(v1,v2)else if(v1 > v2)else }
values2.sort(sort_asc);
alert(values2); //15,10,5,1,0
//對於數值型別或者其valueof()方法返回陣列型別的物件型別
function compare(v1,v2)
//【操作方法】
方法,可以基於當前陣列中的所有項建立乙個新陣列
var colors = ["red","green","blue"];
var colors2 = colors.concat("yellow",["black","brown"]);
alert(colors2); //red,green,blue,yellow,black,brown
//2.擷取陣列
var colors = ["red","green","blue","yellow","purple"];
var colors2 = colors.slice(1); //green,blue,yellow,purple
var colors3 = colors.slice(1,4);//green,blue,yellow
//3.刪除
var array = ["red","green","blue"];
var removed = array.splice(0,1); //刪除第一項
alert(array);//green,blue
alert(removed);//red
//4.新增
removed = array.splice(1,0,"yellow","orange"); //從位置1開發插入2項
alert(array); //green,blue,yellow,orange
alert(removed); //返回乙個空陣列
//5.替換
removed = array.splice(1,1,"red","purple"); //插入兩項,刪除一項
alert(array); //green,red,purple,orange,blue
alert(removed); //yellow
//【位置方法】
//1.查詢某某項在陣列中的位置
var numbers = [1,2,3,4,5,4,3,2,1];
alert(numbers.indexof(4)); //3
alert(numbers.lastindexof(4)); //5
alert(numbers.indexof(4,4)); //5 引數(查詢項,查詢起點位置索引)
var person = ;
var people = ;
var morepeople = [person];
alert(people.indexof(person)); //-1
alert(morepeople.indexof(person)); //0
/*indexof()和lastindexof()方法不支援ie9以下*/
js 中陣列的常用方法總結
arr.fill a 所有子元素 都改為 a array 8 fill 0 後面八個元素 全部用 代替 array 8 fill 0,5 後面5 個元素用 代替,其他是 undefined arr.every item item.age 20 如果陣列中所有元素的age 20 返回true,否則返回...
js 中陣列的常用方法總結
arr.fill a 所有子元素 都改為 a array 8 fill 0 後面八個元素 全部用 代替 array 8 fill 0,5 後面5 個元素用 代替,其他是 undefined arr.every item item.age 20 如果陣列中所有元素的age 20 返回true,否則返回...
js中陣列常用方法總結
1.array.map 此方法是將陣列中的每個元素呼叫乙個提供的函式,結果作為乙個新的陣列返回,並沒有改變原來的陣列。let arr 1 2,3 4,5 let newarr arr.map x x 2 arr 1,2,3,4,5 原陣列保持不變 newarr 2,4,6,8,10 返回新陣列2.a...