JS-16-数组高级API

一、数组高级API上

注意点: 对象中的属性是无序的
for in循环是专门用于遍历对象的, 但是对象的属性是无序的, 所以for in循环就是专门用于遍历无序的东西的, 所以不推荐使用for in循环来遍历数组;

1.利用ES6中推出的for of循环来遍历数组:

       for(let value of arr){
         console.log(value);
       }

2.还可以利用Array对象的forEach方法来遍历数组

forEach方法会自动调用传入的函数,每次调用都会将当前遍历到的元素和当前遍历到的索引和当前被遍历的数组传递给这个函数;

arr.forEach(function (currentValue, currentIndex, currentArray) {
         // console.log(currentValue, currentIndex, currentArray);
         console.log(currentValue);
     });

二、数组高级API中

1.数组的findIndex方法
findIndex方法: 定制版的indexOf, 找到返回索引, 找不到返回-1;

2.数组的find方法
find方法返回索引, find方法返回找到的元素;
find方法如果找到了就返回找到的元素, 如果找不到就返回: undefined

let value = arr.find(function (currentValue, currentIndex, currentArray) {
   // console.log(currentValue, currentIndex, currentArray);
   // if(currentValue === 6){
   if(currentValue === 10){
       return true;
   }
});
console.log(value);

三、数组高级API下

1.数组的filter方法: 将满足条件的元素添加到一个新的数组中;

  let newArray = arr.filter(function (currentValue, currentIndex, currentArray) {
            // console.log(currentValue, currentIndex, currentArray);
            if(currentValue % 2 === 0){  //要求返回偶数
                return true;
            }
        });
   console.log(newArray); // [2, 4]

2.数组的map方法: 将满足条件的元素映射到一个新的数组中;

let newArray = arr.map(function (currentValue, currentIndex, currentArray) {
    // console.log(currentValue, currentIndex, currentArray);
    if(currentValue % 2 === 0){
        return currentValue;
    }
});
console.log(newArray); // [undefined, 2, undefined, 4, undefined]

3.需求: 遍历的同时删除数组中所有元素;

let arr = [1, 2, 3, 4, 5];
for(let i = 0; i < arr.length; i++){
            console.log(arr.length);
            // 注意点: 通过delete来删除数组中的元素, 数组的length属性不会发生变化
            delete arr[i];
        }
        console.log(arr);
四、JavaScript-数组排序方法
let arr = ["c", "a", "b"];
        // arr.sort();

arr.sort(function (a, b) {
            if(a > b){
                return -1;
            }else if(a < b){
                return 1;
            }else{
                return 0;
            }
        });
        console.log(arr);

规律: 如果数组中的元素是数值类型
如果需要升序排序, 那么就返回a - b;
如果需要降序排序, 那么就返回b - a;
// return a - b;
return b - a;

  • 按字符串长度排序:
let arr = ["1234", "21", "54321", "123", "6"];
        arr.sort(function (str1, str2) {
            // return str1.length - str2.length;
            return str2.length - str1.length;
        });
        console.log(arr);
  • 数组中按对象年龄排序:
let students = [
    {name: "zs", age: 34},
    {name: "ls", age: 18},
    {name: "ww", age: 22},
    {name: "mm", age: 28},
];
students.sort(function (o1, o2) {
    // return o1.age - o2.age;
    return o2.age - o1.age;
});
console.log(students);

-End

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值