1、增加

  1. push();
  2. unshift();
  3. concat();
  4. splice(index,0,...value) //从某个下标的前面增加

1.1、push()

向数组后面增加
特点:1、可以增加多个值
使用场景:加载更多 —上拉刷新

  1. <script>
  2. var arr = [1,2,3];
  3. arr.push(4);
  4. arr.push(5,6)
  5. arr.push([7,8,9])
  6. console.log(arr)
  7. </script>

1.2、unshift()

从前增加
使用场景 —历史搜索,下拉刷新

  1. <script>
  2. var arr = [1,2,3];
  3. arr.unshift(0);
  4. console.log(arr)
  5. </script>

1.3、concat()

特点:
1、支持添加多个值
2、支持数组数据格式
3、不会改变数组原本的结构

  1. <script>
  2. var arr = [1,2,3];
  3. var b = arr.concat([4,5]);
  4. console.log(b);
  5. console.log(arr);
  6. var test = [6,7,8]
  7. test.push([4,5]);
  8. console.log(test)
  9. </script>

1.4、indexOf 判断数组中值的下标 、 -1(数组中没有这个值)

  1. <script>
  2. var arr = [1,2,3,4];
  3. console.log(arr.indexOf(4))
  4. console.log(arr.indexOf(3))
  5. console.log(arr.indexOf(8))
  6. </script>

1.5、splice 增加

从下标的前面添加
splice (index,0,value)

  1. <script>
  2. //splice 增加
  3. var arr = [3,4,5,6,7]
  4. arr.splice(2,0,"html","css");
  5. console.log(arr);
  6. </script>

2、删除

2.1、pop() 从后删除

  1. <script>
  2. var arr = [1,2,3];
  3. arr.pop();
  4. console.log(arr);
  5. </script>

2.2、shift() 从前删除

  1. <script>
  2. var arr = [2,3,4];
  3. arr.shift();
  4. console.log(arr);
  5. </script>

2.3、splice 定点删除

splice(index,howmany)
index —从哪个下标开始删除
howmany —删除多少个
从后删除

  1. <script>
  2. var arr = [4,5,6,7,8];
  3. arr.splice(2,1); //从第二个元素开始,删除一个
  4. console.log(arr);
  5. </script>

3、修改

splice(index,howmany,value)
index 下标
howmany 删除多少个
value 替代的值

  1. <script>
  2. var arr = ["html","css","js"];
  3. arr.splice(1,1,"vue");
  4. console.log(arr);
  5. </script>

4、查询

4.1、indexOf 查询数组中值的下标 -1表示数组中没有这个值

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

4.2、slice (startindex,endindex) 不包含结束的下标

  1. var test = arr.slice(1,3);
  2. // slice (startindex,endindex) 不包含结束的下标
  3. console.log(test);

4.3、includes 是否包含某个值

结果返回布尔值

  1. <script>
  2. var arr = [4,5,6];
  3. console.log(arr.includes(4));
  4. console.log(arr.includes(8));
  5. </script>

5、join 拼接

  1. <script>
  2. var arr = ["html","css","js"];
  3. //join 可以将数组拼接成字符串
  4. console.log(arr.join(""));
  5. console.log(arr.join("|"));
  6. console.log(arr.join(" "));
  7. console.log(typeof arr.join(""));
  8. </script>

5.1、事例

  1. <p id="app"></p>
  2. <script>
  3. var directs = [
  4. {name:"吴京",id:1001},
  5. {name:"章子怡",id:1002},
  6. {name:"徐峥",id:1003}
  7. ]
  8. var arr = [ ];
  9. for(var i = 0; i<directs.length; i++){
  10. console.log(directs[i].name);
  11. arr.push(directs[i].name);
  12. console.log(arr);
  13. var str = arr.join("/");
  14. console.log(str)
  15. var app = document.getElementById("app");
  16. app.innerHTML = str;
  17. }
  18. </script>

6、sort 排序

  1. <script>
  2. var arr = [12,3,4,11,1];
  3. console.log(arr.sort());
  4. //得到从小到大的数据结构
  5. var newArr = arr.sort(function(a,b){
  6. return a-b; //升序
  7. })
  8. var c = [12,14,45,2];
  9. var test = c.sort(function(a,b){
  10. return b - a; //降序
  11. })
  12. console.log(test);
  13. console.log(newArr);
  14. </script>

6.1、事例

  1. <script>
  2. var arr = [
  3. {
  4. name:"肯德基",price:100,distance:100
  5. },
  6. {
  7. name:"华莱士",price:200,distance:500
  8. },
  9. {
  10. name:"必胜客",price:50,distance:1000
  11. }
  12. ]
  13. //根据价格进行升序
  14. arr.sort(function(a,b){
  15. return a.price-b.price;
  16. })
  17. console.log(arr);
  18. //根据距离进行降序
  19. arr.sort(function(a,b){
  20. return b.distance - a.distance;
  21. })
  22. console.log(arr);
  23. </script>

7、reverse() 反转颠倒

  1. <script>
  2. var arr = ["html","css","js"]
  3. arr.reverse();
  4. console.log(arr);
  5. </script>

8、for in 遍历

  1. <script>
  2. // for in
  3. var arr = ["html","css","js"]
  4. for(var key in arr){
  5. console.log(key); //key获取的是数组的下标
  6. // console.log(arr[key]);
  7. }
  8. var obj = {
  9. name:"lisi",
  10. age:18
  11. }
  12. for(var k in obj){
  13. console.log(k);
  14. console.log(obj[k]);
  15. }
  16. </script>

9、some 方法 只要数组中有一项满足某个条件,结果就是true

  1. <script>
  2. var arr = [3,4,5,6];
  3. //只要数组中有一项满足某个条件,结果就是true
  4. var res = arr.some(item=>{
  5. return item>7;
  6. })
  7. console.log(res);
  8. </script>

10、map 方法

  1. <script>
  2. var arr = [
  3. {name:"html",value:"12px"},
  4. {name:"css",value:"13px"},
  5. {name:"js",value:"14px"}
  6. ]
  7. arr.map(item => {
  8. var value = parseInt(item.value)*2;
  9. item.value = value;
  10. })
  11. console.log(arr);
  12. </script>

11、filter 过滤 只要达到某个条件,就会返回一个新的数组

  1. <script>
  2. var arr = [3,4,5,6];
  3. var res = arr.filter(item=>{
  4. return item>4;
  5. })
  6. console.log(res);
  7. var students = [
  8. {name:"李四",age:"18岁"},
  9. {name:"李二",age:"11岁"},
  10. {name:"礼物",age:"19岁"},
  11. {name:"王思",age:"22.5岁"}
  12. ]
  13. var result = students.filter(item=>{
  14. return parseFloat(item.age)>11;
  15. })
  16. console.log(result)
  17. </script>