1.将数组 [1,2,3,4,5,6,7,8,9]转换为[[1,2,3],[4,5,6],[7,8,9]]
var arr=[1,2,3,4,5,6,7,8,9];
var sum =[];
for(var i=0;i<arr.length;i=i+3){
var item = arr.slice(i,i+3);
sum.push(item);
}
console.log(sum)
2.将字符串”hello”转换为olleh
/ [h,e,l,l,o] /
// [o,l,l,e,h]
// olleh
var str="hello";
var arr=str.split("");
arr.reverse();
var s=arr.join("");
console.log(s);
3.
将”https://192.168.4.18?lmit=20&offset=0“切割为
[{limit:20},{offset:0}]
var http = "https://192.168.4.18?limit=20&offset=0"
//[{limit:20},{offset:0}]
var arr = http.split("?")[1].split("&");
//["limit=20", "offset=0"]
var res = arr.map(item=>{
var obj = {};
obj[item.split("=")[0]] = item.split("=")[1];
//item = [limit,20]
return obj;
})
console.log(res)
将字符串切割为{limit:20,offset:0}
<script>
var http="https://192.168.4.18?lmit=20&offset=0";
//{limit:20,offset:0}
var arr=http.split("?")[1].split("&")
var obj={};
var res=arr.map(item=>{
obj[item.split("=")[0]]=item.split("=")[1]
// return obj;
})
console.log(obj);
</script>