数组拼接成字符串(join)

  1. var arr = ["html","css","javascript"];
  2. console.log(arr.join());//html,css,javascript
  3. console.log(arr.join(""))//htmlcssjavascript
  4. console.log(arr.join("|"))//html|css|javascript
  5. console.log(arr.join(" "))//html css javascript

例子

  1. <script>
  2. var data = {
  3. country:"美国",
  4. year:1994,
  5. story:"犯罪"
  6. }
  7. var arr = []
  8. for(var i in data){
  9. arr.push(data[i])
  10. }
  11. var str = arr.join("/");
  12. console.log(str);//美国/1994/犯罪
  13. </script>

数组升序、降序(sort)

sort方法会改变原arr的值

升序

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

降序

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

年龄升序排列

  1. <script>
  2. var students = [
  3. {name:"zhang",age:20},
  4. {name:"li",age:18},
  5. {name:"wang",age:9},
  6. {name:"chen",age:14},
  7. ]
  8. /*
  9. 根据年龄升序
  10. 根据年龄降序
  11. */
  12. var result = students.sort((a,b)=>{
  13. return a["age"]-b["age"]
  14. })
  15. console.log(result)
  16. </script>

美团

求和(reduce)

reduce方法不会改变原arr的值

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

取最大\小值Math.max\min(…arr)

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

遍历数组

forEach

forEach方法不会改变原arr的值

  1. <script>
  2. var arr = [2,3,4,5];
  3. arr.forEach((item,index)=>{
  4. console.log(item+"--"+index)
  5. })
  6. </script>

of

  1. var arr = [2,3,4,5]
  2. for(var i of arr){
  3. console.log(i);
  4. }
  1. # of 支持异步循环
  2. function muti(num){
  3. return new Promise((resolve,reject)=>{
  4. setTimeout(()=>{
  5. resolve(num*num)
  6. },1000)
  7. })
  8. }
  9. var arr = [2,3,4];
  10. /*
  11. forEach 是一个同步的循环
  12. of 支持异步循环
  13. */
  14. (async()=>{
  15. /* arr.forEach(async(item)=>{
  16. var res = await muti(item)
  17. console.log(res)
  18. }) */
  19. for(let value of arr){
  20. let res = await muti(value)
  21. console.log(res)
  22. }
  23. })()

map

可以改变数组每一个项,得到一个新的值
1.数组的每一项是基础(简单)类型,执行map不会改变数组原来的结构
2.数组的每一项是复杂类型{object},那么执行map方法会改变原来的数据结构

  1. <script>
  2. var arr = [1,2,3];
  3. var res = arr.map(item=>{
  4. return item*3;
  5. })
  6. console.log(arr);// [1,2,3]
  7. console.log(res);// [3,6,9]
  8. var obj = [{name:"li",age:10},{age:"zhang",age:11}];
  9. var result = obj.map(item=>{
  10. return item.age = 0;
  11. })
  12. console.log(result);
  13. console.log(obj)
  14. </script>

image.png
例子

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

判断

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

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

筛选

  1. var arr = [1,2,3,4];
  2. var res = arr.filter(item=>item>2);
  3. console.log(res)//[3,4]

例子

  1. <script>
  2. var arr = [
  3. {name:"小米",price:1999},
  4. {name:"红米",price:999},
  5. {name:"iphone",price:9999},
  6. {name:"华为",price:3000},
  7. ]
  8. var res = arr.filter(item=>item["price"]>1000)
  9. console.log(res)
  10. </script>

反转

sort方法会改变原arr的值

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

包含

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

对象变为数组

获取对象所有key值

  1. Object.keys();

获取对象所有value值

  1. Object.values();

去重

1.size 获取长度
2.add 添加数据
3.chear() 清除set中所有的数据
4.delete() 删除数据
5.forEach() 遍历
6.has 与includes类似

  1. var arr = [1,2,3,4];
  2. var s = new Set([...arr]);