1、二维数组

二维数组 数组里面套数组

  1. var arr = [[1,2],[3,4]];
  2. console.log(arr[0][1])
  3. console.log(arr[1][0])

2、数组的方法

/ 增,删,改,查 /

增加方法

push();
unshift();
concat();
splice(index,0,…value) //从某个下标的前面增加

增加->push 特点:

  1. 1、可以添加一个值,也可以添加多个值<br /> 2、不能去拼接数组<br /> 3、向后添加
  1. var arr = [1,2,3,4]; //[1,2,3,4,5,6,7]
  2. var b = [5,6,7,8];
  3. arr.push(b);
  4. console.log(arr);

增加->unshift 特点:

  1. 1、向前添加
  1. var arr = [1,2,3,4];
  2. arr.unshift(0);
  3. console.log(arr);

展开

…arr 可以将一个数组展开

  1. var arr = [1,2,3];
  2. console.log(arr);
  3. console.log(...arr);
  4. var a = [4,5,6];
  5. var b = [7,8,9];
  6. a.push(...b);
  7. console.log(a);

concat()方法

用于连接两个或多个字符串

  1. /* concat 不能改变数组结构*/
  2. var arr = [1,2,3];
  3. var c = [ 5,6,7]
  4. var b = arr.concat(c);
  5. console.log(arr);
  6. console.log(b);

删除方法

pop();
shift();
splice(index,howmany);

pop —从末尾删除 shift —从前面删除

  1. var arr = [1,2,3,4];
  2. arr.pop();
  3. arr.shift();
  4. console.log(arr)

定点删除

  1. splice(index,howmany)<br /> index 从哪个下标开始<br /> howmany 删除多少个
  1. var arr = [1,2,3,4,5]; //[1,2,3,5];
  2. arr.splice(3,1);
  3. console.log(arr);

修改方法

splice(index,howmany,…value)

修改splice(index,howmany,value);
index 从哪个下标开始
howmany 删除多少个
value 插入的值(可以是多个值)

  1. var arr = [1,2,3,4,5]; //[1,2,7,4,5]
  2. // arr[2] = 7;
  3. arr.splice(2,1,7);
  4. console.log(arr);

查询

indexOf();
slice(startIndex,endIndex)
includes() //判断数组是否包含某个值

indexOf —查询数组某个值的下标
slice(startIndex,endIndex) 不包含endIndex
slice(startIndex) 截取从startIndex开始到后面所有的值

  1. var arr = [1,2,3,4];
  2. console.log(arr.indexOf(4));
  3. console.log(arr.slice(1,3))
  4. console.log(arr.slice(1))

join()方法

将数组拼接成字符串

  1. var arr = ["html","css","javascript"];
  2. console.log(arr.join());
  3. console.log(arr.join(""))
  4. console.log(arr.join("|"))
  5. console.log(arr.join(" "))
  1. <p id="p"></p>
  2. <script>
  3. var p = document.getElementById("p");
  4. var arr = [1994,"犯罪","剧情"]; // 1994 / 美国 / 犯罪 剧情
  5. var str = arr.join("/");
  6. p.innerHTML = str;
  7. console.log(str);
  8. </script>
  1. var data = {
  2. country:"美国",
  3. year:1994,
  4. story:"犯罪"
  5. }
  6. /* 美国/1994/犯罪 */
  7. var arr = []
  8. for(var i in data){
  9. arr.push(data[i])
  10. }
  11. var str = arr.join("/");
  12. console.log(str);

3、升序降序

升序算法

  1. var arr = [4,7,1,3,14];
  2. arr.sort((a,b)=>{
  3. return a-b;
  4. })
  5. console.log(res)

降序算法

  1. var arr = [4,7,1,3,14];
  2. arr.sort((a,b)=>{
  3. return b-a
  4. })
  5. console.log(res)

例子1:根据年龄升序

  1. var students = [
  2. {name:"zhang",age:20},
  3. {name:"li",age:18},
  4. {name:"wang",age:9},
  5. {name:"chen",age:14},
  6. ]
  7. var result = students.sort((a,b)=>{
  8. return a["age"]-b["age"]
  9. })
  10. console.log(result)

例子2:通过好评数排序

  1. <button id="like">通过好评数排序</button>
  2. <button id="price">通过价格排序</button>
  3. <script>
  4. var like = document.getElementById("like");
  5. var sales = [
  6. { name: "张三", like: 60, distance: 1000, price: 10 },
  7. { name: "张三", like: 30, distance: 100, price: 40 },
  8. { name: "张三", like: 20, distance: 500, price: 6 },
  9. ]
  10. /* 升序 */
  11. function addOrder(arr, value) {
  12. if (Array.isArray(arr)) {
  13. var res = arr.sort((a, b) => {
  14. return a[value] - b[value];
  15. })
  16. return res;
  17. }else{
  18. return null;
  19. }
  20. }
  21. like.onclick = function () {
  22. console.log(addOrder(sales, "like"))
  23. }
  24. price.onclick = function () {
  25. console.log(addOrder(sales, "price"))
  26. }
  27. </script>

求和

  1. var arr = [1,2,3,4];
  2. var sum = 0;
  3. for(var i=0;i<arr.length;i++){
  4. sum =sum+ arr[i];
  5. }
  6. //sum=1
  7. //sum=1+2;
  8. //sum =3+3;
  9. //sum =6+4
  10. console.log(sum)

reduce() 方法

接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。

  1. var arr = [1,2,3,4];
  2. var sum = arr.reduce((a,b)=>{
  3. return a+b;
  4. })
  5. console.log(sum)
  6. console.log(arr);

取最大和最小值

Math.max(…arr) 对数组取最大值
Math.min(…arr) 对数组取最小值

  1. var arr = [1,2,3];
  2. console.log(Math.max(12,1,2));
  3. console.log(Math.max(...arr));

数组的遍历

forEach();
map();
some();
every();
for-of;
filter();
findIndex();
  1. var arr = [2,3,4,5];
  2. // forEach
  3. /*
  4. 1.对数组执行遍历
  5. */
  6. arr.forEach((item,index)=>{
  7. console.log(item+"--"+index)
  8. item+=2;
  9. })

map 可以改变数组每一个项,得到一个新的值

例子1:

  1. /* for-of for in */
  2. var arr = [1,2,3];
  3. for(var i of arr){
  4. // console.log(i)
  5. }
  6. var res = arr.map(item=>{
  7. return item*2
  8. })
  9. console.log(arr);
  10. console.log(res);

例子2:

  1. var arr = [
  2. {name:"html",value:"12px"},
  3. {name:"css",value:"13px"},
  4. {name:"vue",value:"11px"},
  5. ]
  6. /*
  7. var res = [
  8. {name:"html",value:24},
  9. {name:"css",value:26},
  10. {name:"vue",value:22},
  11. ]
  12. */
  13. arr.map(item=>{
  14. var value = parseInt(item["value"])*2;
  15. return item["value"] = value;
  16. })
  17. console.log(arr);

例子3:

  1. var arr = [1,2,3];
  2. /*
  3. 1.数组的每一项是基础(简单)类型,执行map不会改变数组原来的结构
  4. 2.数组的每一项是复杂类型{object},那么执行map方法会改变原来的数据结构
  5. */
  6. var res = arr.map(item=>{
  7. return item*3;
  8. })
  9. console.log(arr);
  10. console.log(res);
  11. var obj = [{name:"li",age:10},{age:"zhang",age:11}];
  12. var result = obj.map(item=>{
  13. return item.age = 0;
  14. })
  15. console.log(result);
  16. console.log(obj)

for in

  1. var arr = [
  2. {name:"html",value:"12px"},
  3. {name:"css",value:"13px"},
  4. {name:"vue",value:"11px"},
  5. ]
  6. var res = []
  7. for( var key in arr){
  8. arr[key].value = parseInt(arr[key].value)*2;
  9. res.push(arr[key]);
  10. }
  11. console.log(res);

// some 返回boolean 只有数组中一项满足条件,就输出true
//every 返回boolean 只有数组中m每一项满足某个条件,就输出true

  1. var arr = [1,2,3,4];
  2. var res = arr.some(item=>{
  3. return item>2;
  4. })
  5. var b = arr.every(item=>item>2);
  6. console.log(res)
  7. console.log(b);

其他

join(seprate)
//升序,降序,求和
arr.sort((a,b)=>a-b);
arr.sort((a,b)=>b-a);
arr.reduce((a,b)=>a+b);
//取最大值
Math.max(…arr);

reverse() //对数组进行反转