js数组字符串常用方法总结

不改变原数组的方法有: concat、join、slice、map

改变原数组的方法:splice、sort 、push、forEach

 1.删除数组中指定的元素  indexOf() & splice() 

    let index = array.indexOf(val)// 获取该元素在数组中的索引
    // 若数组中存在该元素
    if(index > -1){ 
        array.splice(index, 1)
    }

2.字符串去重 indexOf()

let str ='hheello'
let newStr = ''
for(let i = 0; i < str.length; i++){
   if(newStr.indexOf(str[i])==-1){
     newStr+=str[i]
   }
}

3.数组去重

  • Array.filter() + indexOf
  • for...of + includes()

4.数组复制

  • Object.assign() 
  • 扩展运算符
  • array.slice()
  • cocat()

5.向数组中添加元素/数组/合并数组

  • push
  • concat
  • splice
  • unshift(头部添加)
  • Array.prototype.push.apply()

6.数组排序

7.取数组最大值

  • Math.max.apply(null,arr)
  • Math.max(...arr)

8.数组铺平

  • flat(Infinity)展开任意嵌套的深层数组
  • push+concat 递归
  • reduce concat 递归
  • pop push
  • generator yield 递归

9.字符串转数组

 

10.数组转为字符串

  • array.join()

11.遍历数组

  • for...of(该方法可遍历字符串、数组)
  • forEach()
  • map()
  • some()
  • every()
  • filter()
  • reduce()

12.遍历字符串

  • for...of
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值