join()

  1. console.log(arr.join(""))
  2. console.log(arr.join("|"));
  3. console.log(arr.join(" "));
  4. console.log(arr.join("*"));
  5. console.log(typeof arr.join("")) //string
  1. var directs = [
  2. {name:"吴京",id:1001},
  3. {name:"章子怡",id:1002},
  4. {name:"徐峥",id:1003}
  5. ]
  6. /* str =吴京/章子怡/徐峥 */
  7. /* 1. var arr = ["吴京","章子怡","徐峥"] */
  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. }
  13. console.log(arr)
  14. /* 2. str =吴京/章子怡/徐峥*/
  15. var str = arr.join("/");
  16. console.log(str)
  17. /* 3. */
  18. var app = document.getElementById("app");
  19. app.innerHTML = str;

sort()

  1. var arr = [12,3,5,11,1];
  2. console.log(arr.sort())
  3. /* 得到从小到大的数据结构 */
  4. /*
  5. arr.sort(function(a,b){
  6. return a-b;
  7. })
  8. */
  9. var newArr = arr.sort(function(a,b){
  10. return a-b;
  11. })
  12. var c = [12,14,45,2]
  13. /*
  14. 降序
  15. arr.sort(function(a,b){
  16. return b-a;
  17. })
  18. */
  19. var test = c.sort(function(a,b){
  20. return b-a;
  21. })
  22. console.log(newArr)
  23. console.log(test)

image.png

  1. <body>
  2. <select id="mySelect">
  3. <option value="price">低价优先</option>
  4. <option value="distance">距离优先</option>
  5. </select>
  6. <script>
  7. var arr = [{
  8. name:"肯德基",price:100,distance:100
  9. },
  10. {
  11. name:"必胜客",price:200,distance:1500
  12. },
  13. {
  14. name:"华莱士",price:50,distance:1000
  15. }
  16. ]
  17. var mySelect = document.getElementById("mySelect");
  18. // console.log(mySelect)
  19. mySelect.onchange = function(){
  20. console.log(this.value);
  21. var value = this.value;
  22. arr.sort(function(a,b){
  23. return a[value]-b[value];
  24. })
  25. /* 对象的某个属性是变量,不能使用.。只能使用[] */
  26. console.log(arr);
  27. }
  28. </script>
  29. </body>

image.png

reduce()

  1. var arr = [1,2,3];
  2. /*
  3. arr.reduce((a,b)=>a+b)
  4. */
  5. var sum = arr.reduce((a,b)=>{
  6. return a+b;
  7. })
  8. console.log(sum)
  9. //输出6

reverse()

  1. //将数组翻转,可以改变数组结构

for in

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

image.png

for of

  1. var arr = [3,4,5];
  2. for(var value of arr){
  3. // console.log(value)
  4. /* 获取下标怎么获 */
  5. /* indexOf */
  6. console.log(arr.indexOf(value))
  7. }

for each

  1. var names = [{name:"李四"},{name:"王五"}];
  2. names.forEach(item=>{
  3. item.like = true;
  4. })
  5. console.log(names)

image.png

map()

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

image.png

every()

  1. var arr = [2,3,4];
  2. var res = arr.every(item=>{
  3. return item>2;
  4. })
  5. console.log(res)//返回false

some()

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

filter()

  1. /* 只要是达到某个条件,就会返回一个新的数组 */
  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. /* 大于11岁,返回一个新的数组 */
  14. var result = students.filter(item=>{
  15. return parseFloat(item.age)>11;
  16. })
  17. console.log(result)

find()

  1. /* find
  2. 返回数组中满足条件的某个值
  3. */
  4. var arr = [2,3,4,3];
  5. var res = arr.find(item=>{
  6. return item == 3;
  7. })
  8. console.log(res)

findIndex()

  1. /* 返回数组中满足某个条件的值的下标 */
  2. var arr =[2,3,4];
  3. var index = arr.findIndex(item=>{
  4. return item==4;
  5. })
  6. console.log(index)