Blogger Information
Blog 87
fans 1
comment 0
visits 58534
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
字符串与数组常用api学习
阿杰
Original
429 people have browsed it

一、字符串api

  1. let str = 'hello world';
  2. // 长度
  3. console.log('-----------长度-----------');
  4. console.log(str.length);
  5. // 索引->元素
  6. console.log('-----------索引->元素-----------');
  7. console.log(str[1]);
  8. console.log(str.charAt(1));
  9. // 元素->索引
  10. console.log('-----------元素->索引-----------');
  11. console.log(str.indexOf('o'));
  12. console.log(str.search('l'));
  13. // 字符串拼接
  14. console.log('-----------字符串拼接-----------');
  15. console.log(str.concat('<a>', 'php.cn', '</a>'));
  16. console.log(str + 'php.cn');
  17. console.log(`${str}php.cn`);
  18. // 字符串替换
  19. console.log('-----------字符串替换-----------');
  20. console.log(str.replace('world','php中文网'));
  21. // 取子串,忽略结束索引对应的值(a,b)从索引a到索引b
  22. console.log('-----------取子串-----------');
  23. console.log(str.slice(0,2));
  24. console.log(str.slice(1,2));
  25. // 取子串,不知道到哪结束,但知道取多少(a,b)从索引a开始,去b长度
  26. console.log('-----------取子串-----------');
  27. console.log(str.substr(0,2));
  28. console.log(str.substr(2,3));
  29. // 从后往前取
  30. console.log(str.substr(-3,3));
  31. // 字符串变数组
  32. console.log('-----------字符串变数组-----------');
  33. let str2 = '1,2,3,4';
  34. console.log(str2);
  35. console.log(str2.split(','));

二、数组api

(1)数组的增删

  1. // 增删仅限于数组二端的操作
  2. // push,pop, unshift,shift
  3. let arr = [];
  4. console.log(arr);
  5. // 返回数组的元素数量
  6. // push 尾部追加
  7. // pop 尾部删除
  8. console.log('--------------push,pop--------------');
  9. console.log(arr.push(10));
  10. console.log(arr.push(20, 30));
  11. console.log(arr);
  12. // 返回所删除的元素
  13. console.log(arr.pop());
  14. console.log(arr);
  15. // unsift: 头部追加
  16. // shift: 头部删除
  17. console.log('--------------unshift,shift--------------');
  18. console.log(arr.unshift(10));
  19. console.log(arr);
  20. console.log(arr.shift());
  21. console.log(arr);

(2)数组的遍历

  • forEach与map,后者有返回值
  1. // 遍历数组
  2. let arr = ['red','blue','green','yellow','orange'];
  3. // 1、forEach
  4. console.log('--------------forEach--------------');
  5. let res = arr.forEach((item,index,arr)=>{
  6. console.log(item);
  7. })
  8. console.log(res);
  9. // 2、map
  10. console.log('--------------map--------------');
  11. let res2 = arr.map((item,index,arr)=>item);
  12. console.log(res2);

  • every与some
  1. // 3、every
  2. console.log('--------------every--------------');
  3. // every: 数组成员全部满足条件,则返回 true , 否则 false 与
  4. console.log(arr.every(item => item.length >= 0));
  5. console.log(arr.every(item => item.length >= 5));
  6. // 4、some
  7. console.log('--------------every--------------');
  8. console.log(arr.some(item => item.length >= 0));
  9. console.log(arr.some(item => item.length >= 5));

  • filter与find,前者只返回为真的元素组成的数组,后者返回满足条件的第一个
  1. // 5、filter
  2. console.log('--------------filter--------------');
  3. console.log(arr.filter(item => item.length >= 5));
  4. // 6、find
  5. console.log('--------------find--------------');
  6. console.log(arr.find(item => item.length >= 5));

  • reduce
  1. // 7、reduce
  2. console.log('--------------reduce--------------');
  3. let res3 = arr.reduce((acc,item,index,arr) => acc+','+item);
  4. console.log(res3);

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post