1-1 字符串转为数组
var http = "https://192.168.4.18?limit=20&offset=0"
// [{limit:20},{offset:0}]
var arr = http.split("?")[1].split("&")
// console.log(arr); ["limit=20","offset=0"]
var res = arr.map(item=>{
var obj = {}
obj[item.split("=")[0]] = item.split("=")[1]
return obj
})
console.log(res);
1-2 字符串转为对象
var http = "https://192.168.4.18?limit=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]
})
console.log(obj);
1-3 反转字符串
var str = "hello world"
// "dlrow olleh"
var arr = str.split("").reverse().join("")
console.log(arr);
1-4 filter过滤关键字
var arr = [{city:"武汉",state:true,spell:"wuhan"},
{city:"北京",state:false,spell:"beijing"},
{city:"武昌",state:false,spell:"wuchang"}]
var keyword = "wu"
var res = arr.filter(item=>{
if(item.city.includes(keyword) || item.spell.includes(keyword)){
return item
}
})
console.log(res)
1-5 数组去重
var arr = [1,2,1,4,5,6,4];
/* arr = [1,2,4,5,6] */
var result = [];
arr.forEach(item=>{
if(!result.includes(item)){
result.push(item);
}
})
console.log(result)
// set--类数组对象 里面的值不重复
// ... 展开语法也适用于set对象
var s = new Set([2,3,2,1])
console.log(s) // {2,3,1}
var arr = [1,2,1,4,5,6,4]
var s = new Set(arr)
console.log(s) // {1,2,4,5,6}
console.log([...s]) // [1,2,4,5,6]
1-6 对象中键的数组过滤
var obj ={
A:[{name:"html"},{name:"csr"}],
B:[{name:"vue"},{name:"css"}],
C:[{name:"sadd"},{name:"www"}]
}
var arr = Object.values(obj)
console.log(arr) // [[{name:"html"},{name:"csr"}],[{name:"vue"},{name:"css"}],[{name:"sadd"},{name:"www"}]]
var res = []
arr.forEach(item=>{
res.push(...item)
})
console.log(res) // [{name:"html"},{name:"csr"},{name:"vue"},{name:"css"},{name:"sadd"},{name:"www"}]
1-7 对象中关键字过滤
var obj ={
A:[{name:"html"},{name:"csr"}],
B:[{name:"vue"},{name:"css"}],
C:[{name:"sadd"},{name:"www"}]
}
var keyword = "c"
var arr = [];
for(let key in obj){
obj[key].forEach(item=>{
if(item.name.includes(keyword)){
arr.push(item);
}
})
}
console.log(arr);
1-8 使用正则去判断数组和对象
var reg = /^[{\[].+[\]}]$/;
//.所有的字符 + 大于等于一位
console.log(reg.test());