1-1 数组的方法

  1. var arr = [1,2,3,4,5,7] //[1,2,7]
  2. arr.splice(2,3)
  3. console.log(arr);
  4. var arr = [1,2,3,4,5,7] //[1,8,9,7]
  5. arr.splice(1,4,8,9)
  6. console.log(arr);

1-2 ==运算

  1. // Number(""),Number(" "),Number(null) => 输出 0
  2. console.log(false == ""); // true
  3. console.log(true == 1); // true
  4. console.log(null == undefined); // true
  5. console.log(undefined == false); // false
  6. console.log(1 == "1"); // true
  7. /* 1.只有有一边为 boolean 先两边转为number
  8. 2.一边为string,一边为number,先将两遍转为number
  9. // 两边都先转为number
  10. 3. null == undefined
  11. 4. null 和 undefined 不能转化为其他值
  12. */

1-3 join 练习

  1. var data ={
  2. country:"美国",
  3. year:1994,
  4. story:"犯罪"
  5. }
  6. // 美国/1994/犯罪
  7. /* 先将对象转为数组 在进行字符串拼接*/
  8. var arr = []
  9. for(var i in data){
  10. arr.push(data[i])
  11. }
  12. var str = arr.join("/")
  13. console.log(str);

1-4 sort 练习

1-4-1 练习1 根据年龄升序、降序

  1. var students = [
  2. {name:"zhang",age:20},
  3. {name:"li",age:18},
  4. {name:"wang",age:9},
  5. {name:"chen",age:14}
  6. ]
  7. /*
  8. 根据年龄升序
  9. 根据年龄降序
  10. */
  11. var result = students.sort((a,b)=>{
  12. return a["age"]-b["age"]
  13. })
  14. console.log(result);
  15. var result2 = students.sort((a,b)=>{
  16. return b["age"]-a["age"]
  17. })
  18. console.log(result2);

1-4-2 练习2 根据相应的值排序

  1. <button id="like">通过好评数排序</button>
  2. <button id="price">通过价格排序</button>
  3. <script>
  4. var like = document.getElementById("like")
  5. var price = document.getElementById("price")
  6. var sales = [
  7. {name:"张三",like:40,distance:1000,price:10},
  8. {name:"李四",like:60,distance:500,price:50},
  9. {name:"王五",like:30,distance:600,price:40}
  10. ]
  11. /* 升序 */
  12. function addOrder(arr,value){
  13. if(Array.isArray(arr)){
  14. var str = arr.sort((a,b)=>{
  15. return a[value]-b[value]
  16. })
  17. return str
  18. }else{
  19. return null
  20. }
  21. }
  22. like.onclick = function(){
  23. console.log(addOrder(sales,"like"));
  24. }
  25. price.onclick = function(){
  26. console.log(addOrder(sales,"price"));
  27. }
  28. </script>

1-4-3 练习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>

1-5 map 练习

  1. var arr = [
  2. {name:"html",value:"12px"},
  3. {name:"css",value:"13px"},
  4. {name:"vue",value:"11px"}
  5. ]
  6. arr.map(item =>{
  7. var result = parseInt(item["value"])*2
  8. return item["value"] = result
  9. })
  10. console.log(arr);
  11. /* {name: "html", value: 24}
  12. {name: "css", value: 26}
  13. {name: "vue", value: 22} */
  14. /* var res = []
  15. for(var key in arr){
  16. arr[key].value = parseInt(arr[key].value)*2;
  17. res.push(arr[key])
  18. }
  19. console.log(res);
  20. */

1-6 filter 练习

  1. var arr = [
  2. {name:"小米",price:1999},
  3. {name:"红米",price:999},
  4. {name:"iphone",price:9999},
  5. {name:"华为",price:3000},
  6. ]
  7. var res = arr.filter(item =>{
  8. return item["price"]>1000
  9. })
  10. console.log(res);

1-7 输入框获取对应数据

  1. <input type="text" id="input">
  2. <script>
  3. var arr = [
  4. {name:"你是谁,你在哪",id:1102},
  5. {name:"圈住你",id:1102},
  6. {name:"hello在哪里",id:1102},
  7. {name:"怪你",id:1102},
  8. {name:"world",id:1102}
  9. ]
  10. /*
  11. 1.enter回车获取关键字
  12. 2.使用关键字,对数据查询,得到一个新数组
  13. 3.name的字符超过3位以...结尾
  14. */
  15. /* 1.enter回车获取关键字 */
  16. var input = document.getElementById("input")
  17. input.onkeydown = function(event){
  18. if(event.keyCode == 13){
  19. var value = this.value
  20. /* 2.使用关键字,对数据查询,得到一个新数组 */
  21. if(value){
  22. var res = arr.filter(item =>{
  23. return item["name"].includes(value)
  24. })
  25. /* 3.name的字符超过3位以...结尾 */
  26. res.map(item=>{
  27. if(item.name.length>3){
  28. return item.name=item.name.slice(0,3)+"..."
  29. }
  30. })
  31. console.log(res);
  32. }
  33. }
  34. }
  35. </script>

1-8 分割数组

  1. var arr = [1,2,3,4,5,6,7,8,9]
  2. // [1,2,3] [4,5,6] [7,8,9]
  3. // 0 2
  4. // 3 5
  5. // 6 8
  6. var sum = []
  7. for(var i =0;i<arr.length;i=i+3){
  8. var item = arr.slice(i,i+3)
  9. sum.push(item)
  10. }
  11. console.log(sum);

1-9 数组去重

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

1-10 输入id,删除数组中对应的项

  1. <input type="text" id="app">
  2. <script>
  3. var arr = [
  4. {name:"李四",id:1001},
  5. {name:"王四",id:1002},
  6. {name:"张四",id:1003},
  7. {name:"赵四",id:1004}
  8. ]
  9. /*
  10. input输入框,输入谁的id删除谁
  11. */
  12. /*
  13. 1、要获取输入框中的值
  14. 2、根据id值,获取数组中对应项的下标
  15. 3、删除
  16. */
  17. var app = document.getElementById("app");
  18. app.onkeydown = function(event){
  19. if(event.keyCode==13){
  20. var id = Number(this.value);
  21. var index = arr.findIndex(item=>{
  22. return item.id ==id;
  23. })
  24. arr.splice(index,1);
  25. console.log(arr);
  26. }
  27. }
  28. </script>

1-11 关键词过滤

  1. <body>
  2. <input type="text" id="input">
  3. <script>
  4. var searchs = [{
  5. spell: "A",
  6. citys: [{
  7. name: "阿里",
  8. id: 1001
  9. },
  10. {
  11. name: "阿拉",
  12. id: 1001
  13. },
  14. {
  15. name: "阿克",
  16. id: 1001
  17. }
  18. ]
  19. }, {
  20. spell: "T",
  21. citys: [{
  22. name: "天下",
  23. id: 2001
  24. },
  25. {
  26. name: "天天",
  27. id: 2002
  28. },
  29. {
  30. name: "天里",
  31. id: 2003
  32. }
  33. ]
  34. }]
  35. /*
  36. input输入 里
  37. */
  38. // var arr = [
  39. // { name:"天里",id:2003},{name:"阿里",id:1001}
  40. // ]
  41. var keyword = "里";
  42. var arr = [];
  43. var input = document.getElementById("input");
  44. input.onkeydown = function (event) {
  45. if (event.keyCode == 13) {
  46. if (this.value != "") {
  47. console.log(this.value)
  48. var kw = this.value;
  49. addCity(kw);
  50. console.log(arr);
  51. }
  52. }
  53. }
  54. function addCity(keyword) {
  55. for (var i = 0; i < searchs.length; i++) {
  56. var citys = searchs[i].citys;
  57. for (j = 0; j < citys.length; j++) {
  58. // console.log(citys[j])
  59. if (citys[j].name.includes(keyword)) {
  60. arr.push(citys[j]);
  61. }
  62. }
  63. }
  64. }
  65. </script>
  66. </body>