1、合并

  1. <script>
  2. var arr = [
  3. {name:"lisi",
  4. age:18,
  5. likes:["lol","足球"],
  6. alikes:["音乐","电影"],
  7. sex:"male"
  8. },
  9. {name:"ss",
  10. age:11,
  11. likes:["lol","足g球"],
  12. alikes:["音e乐","电影"],
  13. sex:"mafle"
  14. },
  15. {name:"ll",
  16. age:90,
  17. likes:["losl","足s球"],
  18. alikes:["音q乐","电n影"],
  19. sex:"madle"
  20. }
  21. ]
  22. var res = [];
  23. for(var i = 0 ; i<arr.length ; i++){
  24. console.log(arr[i]);
  25. var name = arr[i].name;
  26. var age = arr[i].age;
  27. var likes = arr[i].likes.concat(arr[i].alikes);
  28. res.push(
  29. {
  30. name:name,
  31. age:age,
  32. likes:likes
  33. }
  34. )
  35. }
  36. console.log(res);
  37. </script>

2、合并es6

  1. <script>
  2. var arr = [
  3. {
  4. name: "lisi",
  5. age: 18,
  6. likes: ["lol", "足球"],
  7. alikes: ["音乐", "电影"],
  8. sex: "male"
  9. },
  10. {
  11. name: "ss",
  12. age: 11,
  13. likes: ["lol", "足g球"],
  14. alikes: ["音e乐", "电影"],
  15. sex: "mafle"
  16. },
  17. {
  18. name: "ll",
  19. age: 90,
  20. likes: ["losl", "足s球"],
  21. alikes: ["音q乐", "电n影"],
  22. sex: "madle"
  23. }
  24. ]
  25. var res = [];
  26. for(var i = 0; i<arr.length; i++){
  27. // 解构
  28. var {name,age,likes,alikes} = arr[i];
  29. // 展开
  30. likes.push(...alikes);
  31. // 键和值相同只用写一个
  32. var obj ={
  33. name,
  34. age,
  35. likes
  36. }
  37. res.push(obj);
  38. }
  39. console.log(res)
  40. </script>

3、字符串模板

  1. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  2. </head>
  3. <body>
  4. <script>
  5. var arr = [
  6. { imgUrl:"https://img2.baidu.com/it/u=224367962,4034417299&fm=26&fmt=auto",
  7. name:"cheng",age:19},
  8. { imgUrl:"https://img1.baidu.com/it/u=2848172350,1423562335&fm=26&fmt=auto",
  9. name:"lisi",age:18}
  10. ]
  11. for(var i = 0;i<arr.length;i++){
  12. var {imgUrl,name,age} = arr[i];
  13. var template = `
  14. <div>
  15. <p>${name}</p>
  16. <img src = "${imgUrl}"/>
  17. <p>${age}</p>
  18. </div>
  19. `
  20. console.log(template);
  21. $("body").prepend(template)
  22. }
  23. </script>