一. 数组中的高频方法

数据准备:

let arry1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let arry2 = [
  { id: 1, userName: "ypf1", age: 11 },
  { id: 2, userName: "ypf2", age: 12 },
  { id: 3, userName: "ypf3", age: 13 },
  { id: 4, userName: "ypf4", age: 14 },
  { id: 5, userName: "ypf5", age: 15 },
  { id: 6, userName: "ypf6", age: 16 },
  { id: 7, userName: "ypf2", age: 12 },
];
let arry3 = [11, 12, 13, 14];
let arry4 = [14, 15, 16];

1.  filter

 用于过滤,获取符合条件的数据
用法:条件为true的数据会被放到【新数组里】
/* 
    用于过滤,获取符合条件的数据
    用法:条件为true的数据会被放到【新数组里】
 */
// 案例1:获取所有偶数
{
  let newArry = arry1.filter(item => {
    return item % 2 === 0;
  });
  console.log(newArry);
}
// 案例2:获取age大约13的所有数据
{
  let newArry = arry2.filter(item => item.age > 13);
  console.log(newArry);
}
View Code

相关文章: