每天一篇javascript學習小結(Array數組),javascriptarray

來源:互聯網
上載者:User

每天一篇javascript學習小結(Array數組),javascriptarray

1、數組常用方法

var colors = ["red", "blue", "green"];  //creates an array with three strings    alert(colors.toString());  //red,blue,green    alert(colors.valueOf());   //red,blue,green    alert(colors);        //red,blue,green

2、數組map()方法
 

var numbers = [1,2,3,4,5,4,3,2,1];        var mapResult = numbers.map(function(item, index, array){      //item 數組元素 index元素對應索引 array原數組      console.log(array === numbers);//true      return item * 2;    });    console.log(mapResult);  //[2,4,6,8,10,8,6,4,2]

3、數組reduce()方法

 var values = [1,2,3,4,5];     //接收一個函數,然後從左至右遍曆item,直到reduce到一個值。    var sum = values.reduce(function(prev, cur, index, array){      console.log(array === values);      console.log(index);//1,2,3,4 數組的索引從1開始      return prev + cur;//前後兩個值相加    });    alert(sum);//15

4、數組concat()方法

//concat() 方法用於串連兩個或多個數組。    //該方法不會改變現有的數組,而僅僅會返回被串連數組的一個副本。    //文法    //arrayObject.concat(arrayX,arrayX,......,arrayX)    var colors = ["red", "green", "blue"];    var colors2 = colors.concat("yellow", ["black", "brown"]);        alert(colors);   //red,green,blue        alert(colors2);  //red,green,blue,yellow,black,brown

5、數組長度length

 var colors = new Array(3);   //create an array with three items    var names = new Array("Greg"); //create an array with one item, the string "Greg"    alert(colors.length);//3    alert(names.length);//1
 var colors = ["red", "blue", "green"]; //creates an array with three strings    var names = [];            //creates an empty array    var values = [1,2,];          //AVOID! Creates an array with 2 or 3 items    var options = [,,,,,];         //AVOID! creates an array with 5 or 6 items        alert(colors.length);  //3    alert(names.length);   //0    alert(values.length);  //2 (FF, Safari, Opera) or 3 (IE)    alert(options.length);  //5 (FF, Safari, Opera) or 6 (IE)
var colors = ["red", "blue", "green"];  //creates an array with three strings    colors.length = 2;    alert(colors[2]);    //undefined
var colors = ["red", "blue", "green"];  //creates an array with three strings    colors.length = 4;    alert(colors[3]);    //undefined
 var colors = ["red", "blue", "green"];  //creates an array with three strings    colors[colors.length] = "black";     //add a color    colors[colors.length] = "brown";     //add another color    alert(colors.length);  //5    alert(colors[3]);    //black    alert(colors[4]);    //brown
var colors = ["red", "blue", "green"];  //creates an array with three strings    colors[99] = "black";           //add a color (position 99)    alert(colors.length); //100

6、數組方法every和some

//every()與some()方法都是JS中數組的迭代方法。    //every()是對數組中的每一項運行給定函數,如果該函數對每一項返回true,則返回true。    //some()是對數組中每一項運行指定函數,如果該函數對任一項返回true,則返回true。    var numbers = [1,2,3,4,5,4,3,2,1];        var everyResult = numbers.every(function(item, index, array){      return (item > 2);    });        alert(everyResult);    //false        var someResult = numbers.some(function(item, index, array){      return (item > 2);    });        alert(someResult);    //true

7、數組filter()方法

//從數組中找到適合條件的元素(比如說大於某一個元素的值)    var numbers = [1,2,3,4,5,4,3,2,1];        var filterResult = numbers.filter(function(item, index, array){      return (item > 2);    });        alert(filterResult);  //[3,4,5,4,3]

8、數組indexOf和lastIndexOf

//indexOf() 方法可返回某個指定的字串值在字串中首次出現的位置。    //文法    //stringObject.indexOf(searchvalue,fromindex)    //searchvalue  必需。規定需檢索的字串值。    //fromindex  可選的整數參數。規定在字串中開始檢索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略該參數,則將從字串的首字元開始檢索。       /*    lastIndexOf() 方法可返回一個指定的字串值最後出現的位置,在一個字串中的指定位置從後向前搜尋。    文法    stringObject.lastIndexOf(searchvalue,fromindex)    searchvalue  必需。規定需檢索的字串值。    fromindex  可選的整數參數。規定在字串中開始檢索的位置。它的合法取值是 0 到 stringObject.length - 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    alert(numbers.lastIndexOf(4, 4)); //3        var person = { name: "Nicholas" };    var people = [{ name: "Nicholas" }];    var morePeople = [person];        alert(people.indexOf(person));   //-1    alert(morePeople.indexOf(person)); //0

9、數組toLocaleString和toString

 var person1 = {      toLocaleString : function () {        return "Nikolaos";      },            toString : function() {        return "Nicholas";      }    };        var person2 = {      toLocaleString : function () {        return "Grigorios";      },            toString : function() {        return "Greg";      }    };        var people = [person1, person2];    alert(people);           //Nicholas,Greg    alert(people.toString());      //Nicholas,Greg    alert(people.toLocaleString());   //Nikolaos,Grigorios

10、數組push和pop方法

var colors = new Array();           //create an array    var count = colors.push("red", "green");    //push two items    alert(count); //2        count = colors.push("black");         //push another item on    alert(count); //3        var item = colors.pop();            //get the last item    alert(item);  //"black"    alert(colors.length); //2

11、數組方法unshift和shift

//unshift() 方法可向數組的開頭添加一個或更多元素,並返回新的長度。    //shift() 方法用於把數組的第一個元素從其中刪除,並返回第一個元素的值。    var colors = new Array();           //create an array    var count = colors.unshift("red", "green");  //push two items    alert(count); //2        count = colors.unshift("black");        //push another item on    alert(count); //3        var item = colors.pop();           //get the first item    alert(item);  //"green"    alert(colors.length); //2

12、數組倒序方法reverse

 var values = [1, 2, 3, 4, 5];    values.reverse();    alert(values);    //5,4,3,2,1

13、數組排序方法sort

function compare(value1, value2) {      if (value1 < value2) {        return -1;      } else if (value1 > value2) {        return 1;      } else {        return 0;      }    }        var values = [0, 1, 16, 10, 15];    values.sort(compare);    alert(values);  //0,1,10,15,16    //sort 改變原數組

14、數組方法slice

/*      slice() 方法可從已有的數組中返回選定的元素。      文法      arrayObject.slice(start,end)      start  必需。規定從何處開始選取。如果是負數,那麼它規定從數組尾部開始算起的位置。也就是說,-1 指最後一個元素,-2 指倒數第二個元素,以此類推。      end    可選。規定從何處結束選取。該參數是數組片斷結束處的數組下標。如果沒有指定該參數,那麼切分的數組包含從 start 到數組結束的所有元素。如果這個參數是負數,那麼它規定的是從數組尾部開始算起的元素。      傳回值      返回一個新的數組,包含從 start 到 end (不包括該元素)的 arrayObject 中的元素。    */    var colors = ["red", "green", "blue", "yellow", "purple"];    var colors2 = colors.slice(1);    var colors3 = colors.slice(1,4);        alert(colors2);  //green,blue,yellow,purple    alert(colors3);  //green,blue,yellow

15、數組方法splice

/*      plice() 方法向/從數組中添加/刪除項目,然後返回被刪除的項目。      注釋:該方法會改變原始數組。      文法      arrayObject.splice(index,howmany,item1,.....,itemX)      index  必需。整數,規定添加/刪除項目的位置,使用負數可從數組結尾處規定位置。      howmany  必需。要刪除的項目數量。如果設定為 0,則不會刪除項目。      item1, ..., itemX  可選。向數組添加的新項目。    */    var colors = ["red", "green", "blue"];    var removed = colors.splice(0,1);       //remove the first item    alert(colors);   //green,blue    alert(removed);  //red - one item array        removed = colors.splice(1, 0, "yellow", "orange"); //insert two items at position 1    alert(colors);   //green,yellow,orange,blue    alert(removed);  //empty array    removed = colors.splice(1, 1, "red", "purple");  //insert two values, remove one    alert(colors);   //green,red,purple,orange,blue    alert(removed);  //yellow - one item array

16、數組isArray()方法

alert(Array.isArray([]));  //true    alert(Array.isArray({}));  //false

以上就是今天的javascript學習小結,之後每天還會繼續更新,希望大家繼續關注。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.