博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数组各种方法-读书笔记
阅读量:7238 次
发布时间:2019-06-29

本文共 3254 字,大约阅读时间需要 10 分钟。

1.join()

var colors = ["red", "green", "blue"];alert(colors.join(",")); //red,green,bluealert(colors.join("||")); //red||green||blue

如果不给join()方法传入任何值,或者给它传入undefined,那么则使用逗号作为分隔符。

如果数组中的某一项的值是null或者undefined,那么在join()方法返回的结果中以空字符串表示。

var colors = ["red", "green", "blue",null];colors.join("+");//"red+green+blue+"var colors = ["red", "green", "blue",null,undefined];"red+green+blue++"

2.push()和pop()

var colors = new Array(); // 创建一个数组var count = colors.push("red", "green"); // 推入两项alert(count); //2count = colors.push("black"); // 推入另一项alert(count); //3var item = colors.pop(); // 取得最后一项alert(item); //"black"alert(colors.length); //2

3.shift()和unshift()

var colors = new Array(); //创建一个数组var count = colors.unshift("red", "green"); //推入两项alert(count); //2count = colors.unshift("black"); //推入另一项alert(count); //3var item = colors.pop(); //取得最后一项alert(item); //"green"alert(colors.length); //2 console.log(colors.shift()) //"black" console.log(colors) //"red"

4.reverse()

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

5.sort()

sort()方法可以接收一个比较函数作为参数,以便我们指定哪个值位于哪个值的前面。

比较函数接收两个参数,如果第一个参数应该位于第二个之前则返回一个负数,如果两个参数相等则返回0,如果第一个参数应该位于第二个之后则返回一个正数。以下就是一个简单的比较函数:

//升序 function compare(value1, value2) {    if (value1 < value2) {        return -1;    } else if (value1 > value2) {        return 1;    } else {        return 0;    }}

 var values = [0, 1, 5, 10, 15];

 values.sort(compare);
 alert(values); //0,1,5,10,15

//降序function compare(value1, value2) {    if (value1 < value2) {        return 1;    } else if (value1 > value2) {        return -1;    } else {        return 0;    }}var values = [0, 1, 5, 10, 15];values.sort(compare);alert(values); // 15,10,5,1,0

6.concat()

var colors = ["red", "green", "blue"];var colors2 = colors.concat("yellow", ["black", "brown"]);alert(colors); //red,green,bluealert(colors2); //red,green,blue,yellow,black,brown

7.slice()。

slice()方法可以接受一或两个参数,即要返回项的起始和结束位置。在只有一个参数的情况下,slice()方法返回从该参数指定位置开始到当前数组末尾的所有项。如果有两个参数,该方法返回起始和结束位置之间的项——但不包括结束位置的项。

注:slice()方法不会影响原始数组

var colors = ["red", "green", "blue", "yellow", "purple"];var colors2 = colors.slice(1);var colors3 = colors.slice(1,4);alert(colors2); //green,blue,yellow,purplealert(colors3); //green,blue,yellow

8.splice()

  a.删除:指定两个参数--->要删除的第一项的位置和要删除的项数

var colors = ["red", "green", "blue"];var removed = colors.splice(0,1); // 删除第一项alert(colors); // green,bluealert(removed); // red,返回的数组中只包含一项

  b.插入:提供三个参数:起始位置、要删除的项数和要插入的项数

removed = colors.splice(1, 0, "yellow", "orange"); // 从位置1 开始插入两项alert(colors); // green,yellow,orange,bluealert(removed); // 返回的是一个空数组

  c.替换:提供三个参数:起始位置、要删除的项数和要插入的项数

removed = colors.splice(1, 1, "red", "purple"); // 插入两项,删除一项alert(colors); // green,red,purple,orange,bluealert(removed); // yellow,返回的数组中只包含一项

9.indexOf()和lastIndexOf()

这两个方法都接收两个参数:要查找的项和(可选的)表示查找起点位置的索引。

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

参考资料

《javascript高级程序设计(第3版)》第5章 引用类型

转载于:https://www.cnblogs.com/winteronlyme/p/6699546.html

你可能感兴趣的文章
linux命令---unzip
查看>>
(转)做自己网站的访问记录系统,用于推广统计
查看>>
Chisel语言
查看>>
浅谈Java的学习
查看>>
Mac使用技巧
查看>>
[十二省联考2019]春节十二响
查看>>
【iOS】苹果IAP(内购)中沙盒账号使用注意事项
查看>>
跨域名上传图片
查看>>
XML解析——Java中XML的四种解析方式
查看>>
玩转SpringCloud(F版本) 一.服务的注册与发现(Eureka)
查看>>
Quartz 2D基础知识
查看>>
Servlet常用类
查看>>
leetcode 47全排列II
查看>>
线性/简单DP | 问题集合
查看>>
转 Xenserver HVM is required for this operation的解决办法
查看>>
数据库触发器,禁止DDL操作
查看>>
获取身份证号码信息
查看>>
Python之条件判断
查看>>
Ring0句柄表遍历
查看>>
AC日记——[SDOI2009]HH去散步 洛谷 P2151
查看>>