1.将数组 [1,2,3,4,5,6,7,8,9]转换为[[1,2,3],[4,5,6],[7,8,9]]

  1. var arr=[1,2,3,4,5,6,7,8,9];
  2. var sum =[];
  3. for(var i=0;i<arr.length;i=i+3){
  4. var item = arr.slice(i,i+3);
  5. sum.push(item);
  6. }
  7. console.log(sum)

2.将字符串”hello”转换为olleh

/ [h,e,l,l,o] /
// [o,l,l,e,h]
// olleh

  1. var str="hello";
  2. var arr=str.split("");
  3. arr.reverse();
  4. var s=arr.join("");
  5. console.log(s);

3.

将”https://192.168.4.18?lmit=20&offset=0“切割为
[{limit:20},{offset:0}]

  1. var http = "https://192.168.4.18?limit=20&offset=0"
  2. //[{limit:20},{offset:0}]
  3. var arr = http.split("?")[1].split("&");
  4. //["limit=20", "offset=0"]
  5. var res = arr.map(item=>{
  6. var obj = {};
  7. obj[item.split("=")[0]] = item.split("=")[1];
  8. //item = [limit,20]
  9. return obj;
  10. })
  11. console.log(res)

将字符串切割为{limit:20,offset:0}

  1. <script>
  2. var http="https://192.168.4.18?lmit=20&offset=0";
  3. //{limit:20,offset:0}
  4. var arr=http.split("?")[1].split("&")
  5. var obj={};
  6. var res=arr.map(item=>{
  7. obj[item.split("=")[0]]=item.split("=")[1]
  8. // return obj;
  9. })
  10. console.log(obj);
  11. </script>