1、价格升序,距离降序

  1. var arr = [{
  2. name:"肯德基",price:100,distance:100
  3. },
  4. {
  5. name:"必胜客",price:200,distance:500
  6. },
  7. {
  8. name:"华莱士",price:50,distance:1000
  9. }
  10. ]

2、

  1. var directs = [
  2. {name:"吴京",id:1001},
  3. {name:"章子怡",id:1002},
  4. {name:"徐峥",id:1003}
  5. ]
  6. /* str =吴京/章子怡/徐峥 */

3、

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <select id="mySelect">
  11. <option value="price">低价优先</option>
  12. <option value="distance">距离优先</option>
  13. </select>
  14. <script>
  15. var arr = [{
  16. name:"肯德基",price:100,distance:100
  17. },
  18. {
  19. name:"必胜客",price:200,distance:1500
  20. },
  21. {
  22. name:"华莱士",price:50,distance:1000
  23. }
  24. ]
  25. var mySelect = document.getElementById("mySelect");
  26. // console.log(mySelect)
  27. mySelect.onchange = function(){
  28. console.log(this.value);
  29. var value = this.value;
  30. arr.sort(function(a,b){
  31. return a[value]-b[value];
  32. })
  33. /* 对象的某个属性是变量,不能使用.。只能使用[] */
  34. console.log(arr);
  35. }
  36. </script>
  37. </body>
  38. </html>

4、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:"王五",
  12. age:17,
  13. likes:["al","栏球"],
  14. alikes:["防抖","放到"],
  15. sex:"male"
  16. },
  17. {
  18. name:"张三",
  19. age:165,
  20. likes:["发顺丰","jf"],
  21. alikes:["音乐9","10电影"],
  22. sex:"male"
  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>