Javascript数组学习

  记录下学习数组的过程

  1、创建数组

1 var ary1 = new Array();//空数组
2 var ary2= [] ;//字面量

  2、数组检测

//方法一
if(array instanceof Array){

}

//方法二 ES5
if(Array.isArray(array)){

}
//方法三 
Object.prototype.toString.apply(arrary,arguments);

  3、数组转换方法

//1、toString();
        var ary =['aaaa','vvvvv'];
        ary.toString();//"aaaa,vvvvv"

//2、valueOf();
    var ary =['green','blue','red'];
        ary.valueOf();//["green", "blue", "red"]
//3、toLocaleString();
ary.toLocaleString();//"green,blue,red"
//4、join();
ary.join('@@@&&&&&*****')
//"green@@@&&&&&*****blue@@@&&&&&*****red"

  4、找方法

  ‘后进先出’的表现

ary =['aa','bbb','cccc','dddd'];
//["aa", "bbb", "cccc", "dddd"]
ary.pop();
//"dddd"
ary.push('dddd');
//4
ary
//["aa", "bbb", "cccc", "dddd"]

  5、队列方法

  ‘先进先出’的表现

var ary = ["aa", "bbb", "cccc", "dddd"];
ary.shift();//"aa"
ary.unshift('aaaaaaaaaaaa');//4
console.log(ary);//["aaaaaaaaaaaa", "bbb", "cccc", "dddd"]

  6、循环数组(滚动选择)

1、从后到前
var ary =[10,20,30,40,50,60,70,80,90];
//从后到前
//ary.unshift(ary.pop());
for(var i = 0 ,len =ary.length; i<len;i++){
    console.log(ary);
    ary.unshift(ary.pop());
    console.log(ary);
}

2、从前到后

for(var i = 0 ,len =ary.length; i<len;i++){
    console.log(ary);
    ary.push(ary.shift());
    console.log(ary);
}

  7、数组排序

var ary =[90,100,90,33300,10,20,30,40,50,60,70,80,90];

ary.sort(function(val1,val2){
    return val1-val2;
});
//[10, 20, 30, 40, 50, 60, 70, 80, 90, 90, 90, 100, 33300]
ary.sort(function(val1,val2){
    return val2-val1;
});
//[33300, 100, 90, 90, 90, 80, 70, 60, 50, 40, 30, 20, 10]

  8、数组方法

1、concat();

ary.concat('aaa',['ddd','ccc','eeee']);
//[33300, 100, 90, 90, 90, 80, 70, 60, 50, 40, 30, 20, 10, "aaa", "ddd", "ccc", "eeee"]

2、slice();

var ary1 = [33300, 100, 90, 90, 90, 80, 70, 60, 50, 40, 30, 20, 10, "aaa", "ddd", "ccc", "eeee"];
ary1.slice(0,5);//[33300, 100, 90, 90, 90]
console.log(ary1);//[33300, 100, 90, 90, 90, 80, 70, 60, 50, 40, 30, 20, 10, "aaa", "ddd", "ccc", "eeee"]
ary1.slice(1,3);//[100, 90]

3、splice();最强大的方法

var ary2 = ['a','b','c','d','e','aa','bb','cc']
ary2
//["a", "b", "c", "d", "e", "aa", "bb", "cc"]
ary2.splice(0,0);
//[]
ary2
//["a", "b", "c", "d", "e", "aa", "bb", "cc"]
ary2.splice(1,3);
//["b", "c", "d"]
ary2
//["a", "e", "aa", "bb", "cc"]
ary2.splice(0,0,'a','aaaa');
//[]
ary2
//["a", "aaaa", "a", "e", "aa", "bb", "cc"]

   9、位置方法(ES5)

ary1 = ['222','ffff','sfff','a']
//["222", "ffff", "sfff", "a"]
ary1.indexOf('a');
//3
ary1.indexOf('aaaaa');
//-1

  10、ES5中数组方法(迭代方法)

1.every();数组所有值返回true 返回true

2.some();数组其中有返回true 返回true

3.forEach();数组每一项运行函数,没有返回值

4.map();数组每一项运行函数,返回数组

5.filter();数组每一项运行函数,返回 ture数组

var ary1 = [10,20,5,44,444];
ary1.some(function(value,index){
    return value>100;
});//ture

ary1.every(function(value){
    return value<0;
});//false

ary1.map(function(value,index){
    return value+index;
});//[10, 21, 7, 47, 448]

ary1.filter(function(value,index){
    return value>40;
});//[44, 444]
ary1.forEach(function(value,index){
    ary1[index]=value*2;
});
console.log(ary1);//[20, 40, 10, 88, 888]

 

posted @ 2016-07-14 22:27  牛奶007  阅读(331)  评论(0编辑  收藏  举报

hello world!