• 5.2 Array类型


    实例

    //构造函数
    var arr = new Array();
    l(arr);
    //字面量
    var arr1 = [];
    l(arr1);
    

    数组的length不是只读,可以从数组的末尾移除项或向数组添加新项

    var colors = ['red', 'blue', 'green'];
    l(colors);//(3) ["red", "blue", "green"]
    colors.length = 2;
    l(colors);//(2) ["red", "blue"]
    

    5.2.1 检测数组 (instanceof)

    if(value instanceof Array){}
    if(Array.isArray(value)){}
    if(colors instanceof Array){
    	l('it is array');
    }
    if(Array.isArray(colors)){
    	l('it is array');
    }
    

    5.2.2 转换方法(toLocaleString(),toString(),valueOf(), join())
    toString 以逗号分隔的字符串
    toValue 还是数组
    join 可以特殊分隔符号的字符串
    toLocaleString 方法可根据本地时间把 Date 对象转换为字符串,并返回结果。

    var colors = ['red', 'blue', 'green'];
    alert(colors);//red,blue,green, alert()只能接受字符串参数,所以调用了一次toString()
    l(colors.toString());//red,blue,green
    alert(colors.valueOf())//red,blue,green, 调用了一次toString()
    alert(colors);//red,blue,green, 调用了一次toString()
    l(colors.valueOf());//(3) ["red", "blue", "green"]
    l(colors);//(3) ["red", "blue", "green"]
    l(colors.join(','));//red,blue,green
    l(colors.join(' '));//red blue green
    l(colors.join('|'));//red|blue|gree
    

    5.2.3 栈方法-LIFO[last in first out]后进先出(push(),pop())
    push: 可以接收任意数量的参数,把它们逐个添加到(数组末尾),并返回修改后的数组长度
    pop: 从数组末尾移除最后一项,减少数组length,然后返回移除项

    var colors = new Array();
    var count = colors.push('red','blue');
    l(count);//2
    
    count = colors.push("blue");
    
    var item = colors.pop();
    l(item);//blue
    l(colors.length);//2
    
    
    var colors = ['red', 'blue'];
    colors.push('brown');
    colors[3] = 'black';
    l(colors.length);//4
    var item = colors.pop();
    l(item);//black
    

    5.2.4 队列方法-FIFO[first in first out]先进先出(shift(),push())
    shift: 能够移除数组中第一个项返回该项,同时数组长度减1(ps 和pop移除方式不同,一个头一个尾巴)
    unshift: 能在数组前端添加任意个项,并返回新数组的长度(ps 和push增加方式不同,unshift在头,push在尾)

    var colors = new Array();
    var count = colors.push("red", "green");
    l(count);//2
    count = colors.push('blue');
    l(count)//3
    var item = colors.shift();
    l(item);//red
    l(colors.length)//2
    

    同时使用unshift,可以从相反方向来模拟队列

    var colors = new Array();
    var count = colors.unshift('red', 'green');
    l(count)//2
    count = colors.unshift('black');
    l(count)//3
    
    var item = colors.pop();
    l(item);//green
    l(colors.length);//2
    

    5.2.5 重排序方法(reverse(),sort())
    reverse:翻转数组项的顺序
    sort: (比较字符串比较好)按升序排列数组项,每次调用数组的toString方法转型

    var values = [1,2,3,4,5 ];
    values.reverse();
    l(values);//[5,4,3,2,1]
    
    var values = [1,2,3,4,5 ];
    function compare(value1, value2){
     	if(value1 < value2){
     		return -1;
     	}else if (value1 > value2){
     		return 1
     	}else{
     		return 0;
     	}
     }
    
     values.sort(compare);
     l(values);//[0, 1, 5, 10, 15]
    
    
    //数值,这里相同函数的话,compare会替换上一个
     function compare(value1, value2){
     	return value2 - value1;
     }
    
     values.sort(compare);
     l(values);
    

    5.2.6 操作方法(concat(),slice(),splice()
    concat: 先创建一个副本,然后将接收到的参数添加到这个副本的末尾
    slice: 创建新数组,一或者两个参数(开始和结束位置)[) 如果有负数,就加上数组长度, 第一个参数是下标,第二个参数是数组第几个
    splice: 主要用途就是向数组的中部插入项

    //concat
     var colors = ['red', 'green', 'blue'];
     var colors2 = colors.concat('yellow',['black', 'brown'])
     l(colors);//["red", "green", "blue"]
     l(colors2);//["red", "green", "blue", "yellow", "black", "brown"]
    
    //slice
     var colors = ['red', 'green', 'blue', 'yellow', 'purple'];
     var colors2 = colors.slice(1);
     var colors3 = colors.slice(1, 4);
     l(colors);//["red", "green", "blue", "yellow", "purple"]
     l(colors2);//["green", "blue", "yellow", "purple"]
     l(colors3);//["green", "blue", "yellow"]
    
    //splice
     var colors = ['red', 'green', 'blue'];
     var removed = colors.splice(0, 1);
     l(colors);// ["green", "blue"]
     l(removed);//["red"]
    
     removed = colors.splice(1,0,'yellow','orange');
     l(colors);//["green", "yellow", "orange", "blue"]
     l(removed);//[]
    
     removed = colors.splice(1,1,'red', 'purple');
     l(colors);//["green", "red", "purple", "orange", "blue"]
     l(removed);//["yellow"]
    

    5.2.7 位置方法(indexOf(), lastIndexOf()
    两个参数:要查找的项和查找起点位置的索引 === 返回数组中的位置没找到返回-1
    indexOf: 从数组开头开始查找
    lastIndexOf 从数组末尾向前查找

    var numbers = [1,2,3,4,5,4,3,2,1];
    l(numbers.indexOf(4));//3
    l(numbers.lastIndexOf(4));//5
    l(numbers.indexOf(4,4))//5
    l(numbers.lastIndexOf(4, 4))//3
    
    var person = {name: 'nicholas'};
    var people = [{name: 'nicholas'}];
    var morePeople = [person];
    
    l(people.indexOf(person));//-1
    l(morePeople.indexOf(person));//0
    

    5.2.8 迭代方法(every(), filter(), forEach(), map(), some())
    every:每一项都为true, true (&&)
    filter: true的组成数组
    forEach: 没有返回值
    map: 调用的结果组成数组
    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);
     })
     l(everyResult);//false
     var someResult = numbers.some(function(item, index, array){
     	return (item>2);
     })
     l(someResult);//true
    
     var filterResult = numbers.filter(function(item, index, array){
     	return (item>2);
     })
     l(filterResult);//[3, 4, 5, 4, 3]
    
     var mapResult = numbers.map(function(item, index, array){
     	return (item*2);
     })
     l(mapResult);//[2, 4, 6, 8, 10, 8, 6, 4, 2]
    
     numbers.forEach(function(item, index, array){
     	//一些操作
     })
    

    5.2.9 缩小方法(reduce(),reduceRight())
    迭代数组所有项,然后构建一个最终返回值
    reduce: 从左开始
    reduceRight: 从右开始

     var values = [1,2,3,4,5];
     var sum = values.reduce(function(prev, cur, index, array){
     	l(prev);
     	l(cur);
     	l(index);
     	l(array);
    /*		 	 1
    test.html:10 2
    test.html:10 1
    test.html:10 (5) [1, 2, 3, 4, 5]
    test.html:10 3
    test.html:10 3
    test.html:10 2
    test.html:10 (5) [1, 2, 3, 4, 5]
    test.html:10 6
    test.html:10 4
    test.html:10 3
    test.html:10 (5) [1, 2, 3, 4, 5]
    test.html:10 10
    test.html:10 5
    test.html:10 4
    test.html:10 (5) [1, 2, 3, 4, 5]*/
     	return prev + cur;
     })
     l(sum)//15
    
  • 相关阅读:
    android面试(4)---文件存储
    android面试(3)---基本问题
    android面试(2)----组件
    android面试(1)----布局
    Android四大组件之BroadCast
    Android四大组件之Service(续2)
    Android四大组件之Service(续)
    Android四大组件之Service
    Android四大组件之Activity & Fragement(续)
    172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)
  • 原文地址:https://www.cnblogs.com/caijw/p/8098796.html
Copyright © 2020-2023  润新知