JS 数组方法 Filter Map reduce Find Sort 链接数组方法

1.Filter


const scores = [12, 24, 18, 20, 7, 35, 40];
//过滤filter 方法 会创建一个新的数组
const filterScore = scores.filter(
  (score) => {
    return score >= 20;  //为ture保存为filterScore  不符合跳过
  });

console.log(filterScore); //[24, 20, 35, 40]

const users = [
  { name: 'john', isLogin: true }, 
  { name: 'LuKe', isLogin: false },
  { name: 'Lucy', isLogin: true }, 
  { name: 'Henty', isLogin: true }
];
const LoginUsers=users.filter((user)=>{
  return user.isLogin;
}

);
console.log(LoginUsers)

2.Map

//Map
//、map() ⽅法返回⼀个新数组,新数组中的元素为原始数组中的每个元素调⽤函数处理后得到的值。 不会改变原始数组
const price = [12, 24, 18, 20, 7, 35, 40];
const salePrice=price.map(
  vaule=> vaule/2
);

console.log(salePrice)// [6, 12, 9, 10, 3.5, 17.5, 20]

const products=[
  {names:'app',price:10},
  {names:'orange',price:8},
  {names:'aban',price:20},
  {names:'app',price:10},
  {names:'watermelon',price:5},
]

const saleProducts=products.map(
  product=>{
    if(product.price>=10){
      return {name:product.names,price:product.price/2};
    }else{
      return product;
    }
  }
)

console.log(saleProducts);

reduce方法

//reduce
//、reduce() reduce()方法会遍历数组中的每一个元素,每遍历一次就会执行一次回调函数。当遍历完之后会将最后的结果返回出去。
//
const scores = [12, 24, 18, 20, 7, 35, 40];
//数组判断  计算大于等于35的 数字为几个
const result = scores.reduce(
  (acc, score, index) => {
    if (score >= 35) {
      acc++
    }



    return acc;

  }, 0);

console.log(result) //2

//对象里的属性求和
const score = [
  { student: 'summer', score: 90 },
  { student: 'lucy', score: 100 },
  { student: 'summer', score: 60 },
  { student: 'lu', score: 90 },
  { student: 'summer', score: 90 },

]
const summertotal = score.reduce(
  (acc, curr) => {
    if (curr.student === 'summer') {
      acc += curr.score;
    }
    return acc;
  }, 0
)
console.log(summertotal)// 240


const arr = [1, 2, 3, 4, 5]
const sum = arr.reduce((prev, cur) => {
  return prev + cur
}, 0)
console.log(sum)//15

//3.数组去重

let arrs = [1, 2, 3, 4, 4, 4, 1]
let newArr = arr.reduce((pre, cur) => {
  if (!pre.includes(cur)) {
    return pre.concat(cur);
  } else {
    return pre;
  }
}, [])
console.log('newArr', newArr); //newArr (5) [1, 2, 3, 4, 5]


//计算每个元素出现的 个数

const names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];

let countedNames = names.reduce((pre, name) => {

  if (name in pre) {
    pre[name]++;
  }
  else {
    pre[name] = 1;
  }
  return pre;
}, {});

console.log(countedNames) // {Alice: 2, Bob: 1, Tiff: 1, Bruce: 1}

//4.合并二维数组

const erwei = [[0, 1], [2, 3], [4, 5]].reduce( (pre, cur)=> {
  return pre.concat(cur);
}, []);
console.log(erwei) //[0, 1, 2, 3, 4, 5]

//5.多维数组合并成一维数组
let duowei = [1,3,[4,5,6,[7,8,4]],[7,0,[2,4],9]];
const newArrs =  (val)=> {
  return val.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? newArrs(cur) : cur), []);
}
console.log(newArrs(duowei)) //  [1, 3, 4, 5, 6, 7, 8, 4, 7, 0, 2, 4, 9]

find 方法

const scores=[98,76,55,47,85,100];
// find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

const findScore=scores.find(
  (score)=>{
    return score>90;
  }
);

console.log(findScore); //98   不符合条件返回undefined

sort  reverse  方法

//sort 排序方法
//字符串排序
const names=['summer','lucy','henry','tom'];
names.sort();//排序   ['henry', 'lucy', 'summer', 'tom']
names.reverse();//反转
console.log(names);// ['tom', 'summer', 'lucy', 'henry']


//number排序
const scores=[10,20,11,3,56,32,56];
b 比a 大 进行降序 反之
scores.sort((a,b)=>a-b);
scores.reverse();
console.log(scores);


//对象排序
const students=[
    {student:'summer',score:90 },
    {student:'lucy',score:100 },
    {student:'summer',score:60 },
    {student:'lucy',score:80 },
    {student:'summer',score:80 },

]

//根据值进行排列
// students.sort(
//   (a,b)=>{
//     if(a.score >b.score){
//       return -1; //升序
//     }else if(b.score>a.score){
//       return 1; //降序
//     }else{
//       return 0;
//     }
//   }
// );
  
//b 比a 大 进行降序 反之
students.sort((a,b)=>a.score-b.score);

console.log(students)

链接数组

//链接数组方法
const fruits=[
  {name:'apple',price:10},
  {name:'or',price:8},
  {name:'gr',price:20},
  {name:'watermelon',price:5},
  {name:'peach',price:6},
]
//  获取10以下的 水果 对三个价格进行打折处理 
const filterd=fruits.filter(
  (fruit)=>{
    return fruit.price<10;
  }
);
console.log(filterd) //得到三个过滤的对象

const result=filterd.map(fruit=>{ return `${fruit.name}它的金额为:${fruit.price/2}元`});
console.log(result) // ['or它的金额为:4元', 'watermelon它的金额为:2.5元', 'peach它的金额为:3元']

//第二种写法

const results =fruits.
filter(fruit=>fruit.price<10)
.map(fruit=> `${fruit.name}它的金额为:${fruit.price/2}元`);
console.log(results)// ['or它的金额为:4元', 'watermelon它的金额为:2.5元', 'peach它的金额为:3元']

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值