6 JavaScript Array内置方法


1 数据Array

数组中可以存放所有的数据类型,Array属于object类型

字面量方式创建(推荐大家使用这种方式,简单粗暴)

<script>
    //数组中可以存放所有的数据类型
    var shopping=['香蕉','苹果','牛奶'];
    alert(shopping);
    console.log(shopping); //object类型,Array属于object
    var rand=['tee','123',shopping,[1,23,4]];
    console.log(rand);

    //访问
    var item1=rand[0];
    console.log(item1);
    var a=rand[2][2];
    console.log(a);  //'牛奶'

    //修改
    rand[0]='榴莲';
    console.log(rand);

    //访问数组的长度
    console.log(rand.length);
</script>

使用构造函数(后面会讲)的方式创建 使用new关键词对构造函数进行创建对象,构造函数与后面的面向对象有关系

var colors = new Array();
//通过下标进行赋值
colors[0] = 'red';
colors[1] = 'green';
colors[2] = 'yellow';
console.log(colors);

2 concat()

数组的合并 concat()

var north = ['北京','山东','天津'];
var south = ['东莞','深圳','上海'];

var newCity = north.concat(south);
console.log(newCity)

var colors = ['red','blue'];
var newColors1 = colors.concat('green')
var newColors2 = colors.concat({name:'yellow'})

3 spline()

spline(start,del,items) 删除、插入、替换

var north = ['北京','山东','天津','alex'];
//删除
north.splice(0,2);
console.log(north); //['天津','alex']
//插入
north.splice(1,0,'lxx','wxx');
console.log(north); //['天津','lxx','wxx','alex']
//替换
north.splice(1,1,'姑娘');
console.log(north); //['天津','姑娘','wxx',,'alex']

4 join()

join() 将数组中元素使用指定的字符串连接起来,它会形成一个新的字符串

var score = [98,78,76,100,0];
var str = score.join('|');
console.log(str);  //"98|78|76|100|0"

5 slice()

slice(start,end); 返回数组的一段记录,顾头不顾尾

var arr = ['张三','李四','王文','赵六'];
var newArr  = arr.slice(1,3);
console.log(newArr);//["李四", "王文"]
var newArr  = arr.slice(-3,-1); //相当于(1,3)
console.log(newArr);//['李四',"王文"]

6 pop()

pop 移除数组的最后一个元素

var arr = ['张三','李四','王文','赵六'];
arr.pop();
console.log(arr);//["张三", "李四","王文"]

7 push()

push() 向数组最后添加一个元素

var arr = ['张三','李四','王文','赵六'];
arr.push('小马哥');
console.log(arr);//["张三", "李四","王文","赵六","小马哥"]

8 unshift()

unshift() 向数组最前添加一个元素

var arr = ['张三','李四','王文','赵六'];
arr.unshift('小马哥');
console.log(arr);//["小马哥","张三", "李四","王文","赵六"]

9 shift()

shift() 移除数组的最前一个元素

var arr = ['张三','李四','王文','赵六'];
arr.shift();
console.log(arr);//["李四","王文","赵六"]

10 reverse()

reverse() 翻转数组

var names = ['alex','xiaoma','tanhuang','angle'];
//4.反转数组
names.reverse();
console.log(names);  //["angle", "tanhuang", "xiaoma", "alex"]

11 indexOf()

indexOf() 位置方法,正向查找

var names = ['张三','lxx','王五','lxx','赵六'];
console.log(names.indexOf('lxx'));  //1
console.log(names.indexOf('lxx',2));  //3 从索引2开始查
console.log(names.indexOf('xxx',2));  //查不到结果为-1

12 lastIndexOf()

lastIndexOf() 位置方法,倒序查找

var names = ['张三','lxx','王五','lxx','赵六'];
console.log(names.lastIndexOf('lxx'));  //3
console.log(names.lastIndexOf('lxx',2));  //1 倒序查找索引2开始
console.log(names.lastIndexOf('xxx',2));  //查不到结果为-1

13 sort()

sort对数组排序

var names = ['alex','xiaoma','tanhuang','abngel'];
names.sort();  //默认升序
console.log(names);// ["alex", "angle", "tanhuang", "xiaoma"]
var data = [0,3,2,15,16];
data.sort();  //默认升序
console.log(data); //[0,15,16,2,3]  按照ASCII码排序,先排个位在排十位

function compare1(a,b) {
    /*if (a<b){
        return -1;
    }else if (a>b){
        return 1;
    }else {
        return 0;
    }8*/
    return a-b;
}
data.sort(compare1); //有序的升序排序
console.log(data);  

function compare2(a,b) {
    /*if (a<b){
        return 1;
    }else if (a>b){
        return -1;
    }else {
        return 0;
    }*/
    return b-a;
}
data.sort(compare2); //有序的倒序排序
console.log(data);  

14 isArray()

判断是否为数组:isArray()

 //布尔类型值 = Array.isArray(被检测的变量) ;
 console.log(Array.isArray(score))

15 tostring()

转换为字符串类型:tostring()

var arr = [1,23,33];
var a = arr.toString();
console.log(a)  //1,23,33  用逗号分隔

tostring()和toLocaleString()的区别:

var person1 = {
    toLocaleString : function () {
        return 'lxx';
    },
    toString : function () {
        return 'wxx';
    }
}
var person2 = {
    toLocaleString : function () {
        return 'mjj';
    },
    toString : function () {
        return 'zjj';
    }
}
var people = [person1,person2];
console.log(people);
console.log(people.toString());
console.log(people.toLocaleString());
//打印结果:
//wxx,zjj
//lxx,mjj

16 filter()

迭代方法

filter() 将数组进行过滤

var numbers = [1,2,4,6,13,20];
var filterResult = numbers.filter(function (item,index,array) {
    console.log('item '+item); //item:数据的每一项内容
    console.log('index '+index); //index:表示索引
    console.log('array '+array); //array:原来的数组
    return item > 10;
})
console.log(filterResult);  // 13, 20

17 map()

map() 将数组的每一项元素进行操作

var numbers = [1,2,4,6,13,20];
var mapresult = numbers.map(function (item,index,array) {
    return item * 2;
})
console.log(mapresult);

应用:

var oldArray = [
  {name:'lxx',age:18},
  {name:'mjj',age:28},
  {name:'李四',age:38},
];
var newNames = oldArray.map(function (item,index) {
    return item.name
})
var newAges = oldArray.map(function (item,index) {
    return item.age
})
console.log(newNames);
console.log(newAges);
//输出:
["lxx", "mjj", "李四"]
[18, 28, 38]

18 for

for 数组遍历

方式一:
var numbers = [1,2,4,6,13,20];
for (var i=0; i<mapresult.length; i++){
    console.log(mapresult[i]);
}

方式二:
var arr = [123,234,56]
for (var i in arr){
    console.log(i,arr[i]);
}
//0 123
//1 234
//2 56

19 forEach()

forEach() 遍历操作

var numbers = [1,2,4,6,13,20];
numbers.forEach(function (item,index,array) {
    console.log(item);
})

20 split()

split() 分割字符串

var s = 'where is how';
var r = s.split(' ');
console.log(r); // ["where", "is", "how"]

21 includes()

includes() 判断数据是否在数组中存在

var num = [1,2,3,4];
console.log(num.includes(2)); //true

22 reduce

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/279050.html

(0)
上一篇 2022年8月5日
下一篇 2022年8月5日

相关推荐

发表回复

登录后才能评论