1. <script>
  2. var obj = {
  3. name:"lisi",
  4. age:18
  5. }
  6. var arr = [
  7. {name:"lisi",age:14}
  8. ]
  9. // var res = JSON.stringify(obj);
  10. console.log(JSON.stringify(obj))
  11. console.log(JSON.stringify(arr))
  12. var str = `{"name":"李四","age":18}`
  13. var sum =JSON.parse(str)
  14. // json.parse可以将json格式的字符串转换为json对象
  15. console.log(sum)
  16. </script>

示例

  1. <script>
  2. var str = `
  3. [{"name":"李四666","data":[2019,10,11]},
  4. {"name":"张三666","data":[2019,10,11]},
  5. {"name":"王五点击666","data":[2019,10,11]}]
  6. `
  7. /*
  8. var arr = [
  9. {name:"李四...",data:"2019-10-11"}
  10. ]
  11. */
  12. var arr =JSON.parse(str)
  13. var list = [];
  14. arr.forEach(item=>{
  15. var {name,data}=item;
  16. name = name.replace(/\d/g,"")+"...";
  17. data = data.join("-")
  18. list.push({
  19. name,
  20. data
  21. })
  22. })
  23. console.log(list)
  24. //console.log(str.replace(reg,""))
  25. </script>
  1. <script>
  2. var arr =[
  3. {name:"lisi"},
  4. {name:"html"},
  5. {name:"css"},
  6. {name:"vue"},
  7. {name:"vue"},
  8. {name:"html"},
  9. ]
  10. // ["lisi","html","css","vue"]
  11. var list = []
  12. arr.forEach(item=>{
  13. var {name} = item;
  14. if(!list.includes(name)){
  15. list.push(name);
  16. }
  17. })
  18. console.log(list)
  19. </script>
  1. <script>
  2. var arr ={
  3. A:[{city:"武汉"},{city:"广州"}],
  4. B:[{city:"深圳"},{city:"广州"}],
  5. C:[{city:"上海"},{city:"武汉"}],
  6. }
  7. var list=[];
  8. for(var key in arr){
  9. console.log(arr[key])
  10. list.push(...arr[key]);
  11. }
  12. //console.log(list)
  13. var res =[];
  14. list.forEach(item=>{
  15. var {city} = item;
  16. console.log(item)
  17. if(!res.includes(city)){
  18. res.push(city);
  19. }
  20. })
  21. console.log(res)
  22. </script>