Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数组方法的总结 #3

Open
wangjbo opened this issue Jan 21, 2022 · 0 comments
Open

数组方法的总结 #3

wangjbo opened this issue Jan 21, 2022 · 0 comments

Comments

@wangjbo
Copy link
Owner

wangjbo commented Jan 21, 2022

array数组的常用方法:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator#

filter

  1. filter 不会改变原来传入的数组,但是会返回一个新的数组
  2. quick start
    const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    const result = words.filter(word => word.length > 6);
    console.log(result);
    // expected output: Array ["exuberant", "destruction", "present"]
    
  3. 一个特殊的情况。数组后面 filter(boolean)
    let a=[1,2,"b",0,{},"",NaN,3,undefined,null,5];
    let b = a.filter(Boolean)  // [1,2,"b",{},3,5]
    //  相当于对数组的每一项使用Boolean进行操作,返回true的项就会保留
    //  等价于
    a.filter(item => { return Boolean(item)})
    

charAt 和 indexOf

  1. charAt:寻找第几个下标的数字
  2. indexOf: 寻找某个指定字符的下标

concat

  1. 拼接字符串,会先将string转化为Sting 包装类,然后拼接,最后返回 拼接好的字符串,并且不改变之前的字符串
  2. 一般不会用concat,而是直接用 + 来进行拼接,需要注意的是,只有用 + 运算符将string类型和其他类型相加的时候,会将其他类型隐式转化为 string,如果 ‘123’-‘1’ 返回的结果是 number类型的 122
  3. 谈到将string 转化为 number,有以下几种方法
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant