1、以…结尾

1.1、超过六位

  1. <script>
  2. //超过六位以...结尾
  3. var str = "时间有限来不及去惦记心里没有我的人"
  4. console.log(str.length);
  5. if(str.length>6){
  6. var res = str.slice(0,6)+"..."; //var res = str.substr(0,6)+"...";
  7. console.log(res);
  8. }
  9. </script>

1.2、超过三位

  1. <script>
  2. var arr = [
  3. "时间有限来",
  4. "及去惦记心里",
  5. "及去惦记心里没有我的人",
  6. "你好"
  7. ]
  8. var res = [];
  9. arr.forEach(item=>{
  10. if(item.length>3){
  11. item = item.substr(0,3)+"...";
  12. }
  13. res.push(item);
  14. })
  15. console.log(res);
  16. </script>

1.3、map方法实现:

  1. <script>
  2. var arr = [
  3. "时间有限来",
  4. "及去惦记心里",
  5. "及去惦记心里没有我的人",
  6. "你好"
  7. ]
  8. var res = arr.map(item=>{
  9. if(item.length>3){
  10. item = item.substr(0,3)+"...";
  11. }
  12. return item
  13. })
  14. console.log(res);
  15. </script>

1.4、把含有“你”的字符串超过三位以…结尾

  1. <input type="text" id="app">
  2. <script>
  3. var arr = [
  4. "时间有限来",
  5. "及去惦你记心里",
  6. "及去惦记心里没有我的人",
  7. "你好"
  8. ]
  9. $("#app").keyup(function(event){
  10. if(event.keyCode == 13){
  11. var value = $(this).val();
  12. var res = arr.filter(item=>{
  13. return item.includes(value);
  14. })
  15. var lists = res.map(item=>{
  16. if(item.length>3){
  17. item = item.substr(0,3)+"...";
  18. }
  19. return item;
  20. })
  21. console.log(lists);
  22. }
  23. })
  24. </script>

1.5、通过id搜索,最后以…结尾

  1. <input type="text" id="app">
  2. <script>
  3. var arr = [
  4. {id:1001,name:"fslkhlasl"},
  5. {id:1002,name:"bkhaoiuohf"},
  6. {id:1003,name:"hiuios"}
  7. ]
  8. //1.获取input的值
  9. $("#app").keyup(function (event) {
  10. if (event.keyCode == 13) {
  11. var value = $(this).val();
  12. var res = arr.find(item=>{
  13. return item.id == value;
  14. })
  15. res.name = res.name +"...";
  16. var list = [res];
  17. console.log(list);
  18. }
  19. })
  20. </script>

2、字符串分割数组反转

  1. <script>
  2. var str = "hello";
  3. var arr = str.split("")
  4. console.log(arr);
  5. arr.reverse();
  6. console.log(arr.join(""));
  7. </script>

3、?key=你&age=18

  1. <script>
  2. var str = "?key=你&age=18"
  3. var res = str.slice(1);
  4. var list = res.split("&");
  5. console.log(list);
  6. var key = list[0];
  7. var age = list[1];
  8. key = key.split("=")[1];
  9. age = age.split("=")[1];
  10. var obj = {
  11. key,
  12. age,
  13. }
  14. console.log(obj);
  15. </script>

3.1、”?key=你&age=18&offset=10”

  1. <script>
  2. var list = [];
  3. var str = "?key=你&age=18&offset=10";
  4. //[{key:"你"},{age:18},{offset:10}]
  5. var arr = str.slice(1).split("&");
  6. arr.forEach(item=>{
  7. var res = item.split("=");
  8. var obj = {};
  9. obj[res[0]]=res[1];
  10. list.push(obj);
  11. })
  12. console.log(list);
  13. </script>

3.2、map 方法 “?key=你&age=18&offset=10”

  1. <script>
  2. var str = "?key=你&age=18&offset=10";
  3. var arr = str.slice(1).split("&");
  4. console.log(arr);
  5. var res = arr.map(item=>{
  6. var obj = {};
  7. var list = item.split("=");
  8. obj[list[0]] = list[1];
  9. console.log(obj);
  10. return obj;
  11. })
  12. console.log(res);
  13. </script>

3.3、”https://www.baidu.com?order=hot&cat=你&limit=35&offset=140

  1. <script>
  2. var url = "https://www.baidu.com?order=hot&cat=你&limit=35&offset=140";
  3. var res = url.split("?")[1].split("&");
  4. console.log(res);
  5. var obj = {};
  6. //让数组的每一项中,第一项作为key,第二项作为value
  7. res.forEach(item=>{
  8. var list = item.split("=");
  9. obj[list[0]] = list[1];
  10. })
  11. console.log(obj);
  12. </script>