findIndex

272 阅读1分钟
 // findIndex  符合条件的元素的下标!!
      // 用法可以find很类似  在函数中 如果找到了 返回true
      // 找不到 就返回 -1 
      const arr = [
        { username: '悟空', height: 70 },
        { username: '八戒', height: 60 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
      ];
      // 帮我找到 身高为60的那一个元素 
      const index = arr.findIndex((value) => value.height === 660);// 找不到就返回 -1 
      console.log(index);
      // 帮我删除它!!
     // arr.splice(index,1);
      // console.log(arr);

image.png