1-1 字符串转为数组

  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. // console.log(arr); ["limit=20","offset=0"]
  5. var res = arr.map(item=>{
  6. var obj = {}
  7. obj[item.split("=")[0]] = item.split("=")[1]
  8. return obj
  9. })
  10. console.log(res);

1-2 字符串转为对象

  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. var obj = {}
  5. var res = arr.map(item=>{
  6. obj[item.split("=")[0]] = item.split("=")[1]
  7. })
  8. console.log(obj);

1-3 反转字符串

  1. var str = "hello world"
  2. // "dlrow olleh"
  3. var arr = str.split("").reverse().join("")
  4. console.log(arr);

1-4 filter过滤关键字

  1. var arr = [{city:"武汉",state:true,spell:"wuhan"},
  2. {city:"北京",state:false,spell:"beijing"},
  3. {city:"武昌",state:false,spell:"wuchang"}]
  4. var keyword = "wu"
  5. var res = arr.filter(item=>{
  6. if(item.city.includes(keyword) || item.spell.includes(keyword)){
  7. return item
  8. }
  9. })
  10. console.log(res)

1-5 数组去重

  1. var arr = [1,2,1,4,5,6,4];
  2. /* arr = [1,2,4,5,6] */
  3. var result = [];
  4. arr.forEach(item=>{
  5. if(!result.includes(item)){
  6. result.push(item);
  7. }
  8. })
  9. console.log(result)
  10. // set--类数组对象 里面的值不重复
  11. // ... 展开语法也适用于set对象
  12. var s = new Set([2,3,2,1])
  13. console.log(s) // {2,3,1}
  14. var arr = [1,2,1,4,5,6,4]
  15. var s = new Set(arr)
  16. console.log(s) // {1,2,4,5,6}
  17. console.log([...s]) // [1,2,4,5,6]

1-6 对象中键的数组过滤

  1. var obj ={
  2. A:[{name:"html"},{name:"csr"}],
  3. B:[{name:"vue"},{name:"css"}],
  4. C:[{name:"sadd"},{name:"www"}]
  5. }
  6. var arr = Object.values(obj)
  7. console.log(arr) // [[{name:"html"},{name:"csr"}],[{name:"vue"},{name:"css"}],[{name:"sadd"},{name:"www"}]]
  8. var res = []
  9. arr.forEach(item=>{
  10. res.push(...item)
  11. })
  12. console.log(res) // [{name:"html"},{name:"csr"},{name:"vue"},{name:"css"},{name:"sadd"},{name:"www"}]

1-7 对象中关键字过滤

  1. var obj ={
  2. A:[{name:"html"},{name:"csr"}],
  3. B:[{name:"vue"},{name:"css"}],
  4. C:[{name:"sadd"},{name:"www"}]
  5. }
  6. var keyword = "c"
  7. var arr = [];
  8. for(let key in obj){
  9. obj[key].forEach(item=>{
  10. if(item.name.includes(keyword)){
  11. arr.push(item);
  12. }
  13. })
  14. }
  15. console.log(arr);

1-8 使用正则去判断数组和对象

  1. var reg = /^[{\[].+[\]}]$/;
  2. //.所有的字符 + 大于等于一位
  3. console.log(reg.test());