todolist

  1. <body>
  2. <input type="text" id="input">
  3. <h2>历史搜索</h2>
  4. <div id="app">
  5. </div>
  6. <script>
  7. var input = document.getElementById("input");
  8. var app = document.getElementById("app");
  9. var historys = []
  10. /* 1、获取输入框的值 */
  11. input.onkeydown = function (event) {
  12. if (event.keyCode == 13) {
  13. // console.log(this.value)
  14. /* 2、只要数组中没有的值同时字符不能空,才向数组添加 */
  15. if (this.value != "" && historys.indexOf(this.value) == -1) {
  16. var p = document.createElement("p");
  17. p.innerHTML = this.value;
  18. app.prepend(p);
  19. historys.unshift(this.value);
  20. this.value = ""
  21. console.log(historys)
  22. }else{
  23. alert("输入的字段不能是空字符或相同的字段")
  24. }
  25. }
  26. }
  27. </script>
  28. </body>

求数组的和

  1. <script>
  2. var arr = [1,2,3,4,5,8]
  3. var sum = 0;
  4. for(var i = 0;i<arr.length;i++){
  5. sum +=arr[i];
  6. }
  7. console.log(sum);
  8. </script>

大于16岁组成一个新数组

  1. <script>
  2. // age >16
  3. var arr = [
  4. {name:"李四" , age:"18岁"},
  5. {name:"李四" , age:"20岁"},
  6. {name:"李四" , age:"8岁"},
  7. {name:"李四" , age:"16岁"},
  8. ]
  9. var test = [];
  10. for(var i = 0; i<arr.length; i++){
  11. var age = parseInt(arr[i].age);
  12. if(age>16){
  13. console.log(arr[i]);
  14. test.push(arr[i]);
  15. }
  16. }
  17. console.log(test);
  18. </script>

image.png

一个数组分成三个数组

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

image.png

求最小值

  1. <script>
  2. var arr = [4,6,18,1];
  3. var min = arr[0];
  4. for(var i=1; i<arr.length; i++){
  5. if(min>arr[i]){
  6. min = arr[i];
  7. }
  8. }
  9. console.log(min);
  10. </script>

求最大最小值

Math方法

  1. var arr = [4,5,6];
  2. // Math.max = arr[0];
  3. // for(var i=1;i<arr.length;i++){
  4. // if(arr[i]>max){
  5. // max = arr[i];
  6. // }
  7. // }
  8. // console.log(max)
  9. console.log(Math.max(...arr))
  10. console.log(Math.min(...arr))

sort

一、根据价格进行升序 —从小到大

<script>
        var arr = [{
            name:"肯德基",price:100,distance:100
        },
        {
            name:"必胜客",price:200,distance:500
        },
        {
            name:"华莱士",price:50,distance:1000
        }
    ]
    /* 根据价格进行升序 --从小到大 */
    arr.sort(function(a,b){
        return a.price-b.price;
    })
    console.log(arr);
    /* 根据距离进行降序 --从大到小 */
    arr.sort(function(a,b){
        return b.distance-a.distance;
    })
    console.log(arr);
    </script>

image.png

二、低价优先 距离优先

<body>
    <select  id="mySelect">
        <option value="price">低价优先</option>
        <option value="distance">距离优先</option>
    </select>
    <script>
         var arr = [{
            name:"肯德基",price:100,distance:100
        },
        {
            name:"必胜客",price:200,distance:1500
        },
        {
            name:"华莱士",price:50,distance:1000
        }
    ]
    var mySelect = document.getElementById("mySelect");
    // console.log(mySelect)
    mySelect.onchange = function(){
        console.log(this.value);
        var value = this.value;
        arr.sort(function(a,b){
            return a[value]-b[value];
        })
        /* 对象的某个属性是变量,不能使用.。只能使用[] */
        console.log(arr);
    }
    </script>
</body>

例子 - 图4

对象的某个属性是变量,不能使用. 只能使用[]

<script>
        //对象的某个属性是变量,不能使用.  只能使用[]
        var obj = {
            name:"lisi",
            age:18
        }
        var value = "name";
        console.log(obj[value]);
    </script>

sum+=arr[i].price*arr[i].num;

 <script>
         var arr = [{
            name:"肯德基",price:100,distance:100,num:2
        },
        {
            name:"必胜客",price:200,distance:1500,num:1
        },
        {
            name:"华莱士",price:50,distance:1000,num:5
        }
    ]
    /* sum */
    var sum = 0;
    for(var i=0;i<arr.length;i++){
        sum+=arr[i].price*arr[i].num;
    }
    console.log(sum)
    </script>

join 例子

一、吴京/章子怡/徐峥

<body>
    <p id="app"></p>
    <script>
        var directs = [
            {name:"吴京",id:1001},
            {name:"章子怡",id:1002},
            {name:"徐峥",id:1003}
        ]
        /* str =吴京/章子怡/徐峥 */
        /* 1.  var arr = ["吴京","章子怡","徐峥"] */
        var  arr = [ ];
        for(var i=0;i<directs.length;i++){
            console.log(directs[i].name)
            arr.push(directs[i].name)
        }
        console.log(arr)
        /* 2. str =吴京/章子怡/徐峥*/
        var str = arr.join("/");
        console.log(str)
        /* 3. */
        var app = document.getElementById("app");
        app.innerHTML = str;
    </script>
</body>

image.png

二、2019-03-09

<body>
    <div id="app"></div>
    <script>
        var dates = ["2019","03","09"]
        /* 2019-03-09 */
        var str = dates.join("-");
        console.log(str)
        var app = document.getElementById("app");
        app.innerHTML =str;
    </script>
</body>

forEach

一、newArr.push(item*2);

<script>
        var arr = [2,3,4];
        var newArr = [];
        arr.forEach(item=>{
            console.log(item)
            newArr.push(item*2);
        })
        console.log(newArr)
    </script>

二、”24-26-28”

<script>
        var arr = [
            {name:"html",value:"12px"},
            {name:"css",value:"13px"},
            {name:"js",value:"14px"}
        ]
        /* 
        var nums = [12,13,14]
        [24,26,28]
        "24-26-28"
         */
        var nums = [];
        arr.forEach(item=>{
            var value = parseInt(item.value)*2;
            nums.push(value);
        })
        console.log(nums)
        var str = nums.join("-");
        console.log(str)
    </script>

map 方法x2

<script>
        var arr = [
            {name:"html",value:"12px"},
            {name:"css",value:"13px"},
            {name:"js",value:"14px"}
        ]
        arr.map(item=>{
            var value = parseInt(item.value)*2;
            item.value = value;
        })
        console.log(arr)
    </script>

image.png

filter

一、大于11岁

<script>
        /* 只要是达到某个条件,就会返回一个新的数组 */
        var students = [
            {name:"李四",age:"18岁"},
            {name:"李二",age:"11岁"},
            {name:"李五",age:"19岁"},
            {name:"王四",age:"22.5岁"},
        ]
        /* 大于11岁,返回一个新的数组 */
        var result = students.filter(item=>{
            return parseFloat(item.age)>11;
        })
        console.log(result)
    </script>

image.png

二、输入李

<body>
    <input type="text" id="app">
    <script>

         var students = [
            {name:"李四",age:"18岁"},
            {name:"李二",age:"11岁"},
            {name:"李五",age:"19岁"},
            {name:"王四",age:"22.5岁"},
        ]
        var app = document.getElementById("app");
        app.onkeydown = function(event){
            if(event.keyCode == 13){
                var keyword = this.value;
                console.log(keyword)
                var res = students.filter(item=>{
                    return item.name.includes(keyword);
                })
                console.log(res)
            }
        }
    </script>
</body>

例子 - 图8

关键词过滤

input输入 里,搜索所有有里字的

<input type="text" id="input">
    <script>
        var searchs = [{
            spell: "A",
            citys: [{
                    name: "阿里",
                    id: 1001
                },
                {
                    name: "阿拉",
                    id: 1001
                },
                {
                    name: "阿克",
                    id: 1001
                }
            ]
        }, {
            spell: "T",
            citys: [{
                    name: "天下",
                    id: 2001
                },
                {
                    name: "天天",
                    id: 2002
                },
                {
                    name: "天里",
                    id: 2003
                }
            ]
        }]
        /*
        input输入 里 
         */
        //    var arr = [
        //        {    name:"天里",id:2003},{name:"阿里",id:1001}
        //    ]

        var keyword = "里";
        var arr = [];
        var input = document.getElementById("input");
        input.onkeydown = function (event) {
            if (event.keyCode == 13) {
                if (this.value != "") {
                    console.log(this.value)
                    var kw = this.value;
                    addCity(kw);
                    console.log(arr);
                }
            }
        }
        function addCity(keyword) {
            for (var i = 0; i < searchs.length; i++) {
                var citys = searchs[i].citys;
                for (j = 0; j < citys.length; j++) {
                    // console.log(citys[j])
                    if (citys[j].name.includes(keyword)) {
                        arr.push(citys[j]);
                    }
                }
            }
        }
    </script>

另一种方法

var arr = [];
        var input = document.getElementById("input");
        /* 1、获取关键字 */
        input.onkeydown = function (event) {
            if (event.keyCode == 13) {
                if (this.value != "") {
                    var kw = this.value;
                    console.log(kw);
                    /* 2、使用关键字对数组进行遍历 */
                    for (var i = 0; i < searchs.length; i++) {
                        var citys = searchs[i].citys;
                        for (j = 0; j < citys.length; j++) {
                            // console.log(citys[j])
                            /* 3、获取包含关键字的项目 */
                            if (citys[j].name.includes(kw)) {
                                arr.push(citys[j]);
                            }
                        }
                    }
                    console.log(arr)
                }
            }
        }

input输入框,输入谁的id删除谁

<body>
    <input type="text" id="app">
    <script>

        var arr = [
            {name:"李四",id:1001},
            {name:"王四",id:1002},
            {name:"张四",id:1003},
            {name:"赵四",id:1004}
        ]
        /* 
        input输入框,输入谁的id删除谁
         */
       /* 
       1、要获取输入框中的值
       2、根据id值,获取数组中对应项的下标
       3、删除
        */
       var app = document.getElementById("app");
       app.onkeydown = function(event){
           if(event.keyCode==13){
               var id = Number(this.value);
               var index = arr.findIndex(item=>{
                   return item.id ==id;
               })
               arr.splice(index,1);
               console.log(arr);
           }
       }
    </script>
</body>

历史搜索

<body>
    <h2>历史搜索</h2>
    <input type="text" id="app">
    <div id="container">

    </div>
    <script>
        /* 1、获取输入框的值 */
        var historys = [];
        $("#app").keyup(function (event) {
            if (event.keyCode == 13) {
                /* 2.将值添加到一个数组里面去 */
                var value = $(this).val();
                var template = `
                       <button>${value}</button>
                 `
                /* 只有数组中不包含输入的关键字才向数组添加 */
                if (value && !historys.includes(value)) {
                    historys.unshift(value);
                    console.log(historys)
                    /* 3、渲染数据到页面 */

                    $("#container").prepend(template)
                    $(this).val("")
                } else {
                    /* 4.当输入的值数组中存在的时候,让其至于数组的首位 */
                    console.log(value)
                    var index = historys.indexOf(value);
                    var res = historys.splice(index, 1);
                    historys.unshift(value);
                    console.log(historys)
                    /* ui联动 */
                    $("#container button").eq(index).remove();
                    $("#container").prepend(template)
                }

            }
        })
    </script>
</body>

image.png

 <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>

<input type="text" id="input">
    <h2>历史搜索</h2>
    <div id="container">

    </div>
    <script>
        var historys = [ ];
        /* 1、enter获取输入框的值 */
        $("#input").keyup(function(event){
            if(event.keyCode==13){

                var value = $(this).val();
                if(value){
                    /* 2、只有数组中不包含搜索的值,才添加 */
                    if(!historys.includes(value)){
                        historys.unshift(value);
                        $("#container").prepend(`<button>${value}</button>`)
                        console.log(historys)
                        $("#input").val("")
                    }else{
                        /* 3、将数组中存在的值,至于数组的顶部 */
                        console.log(value)
                        var index = historys.indexOf(value);
                        historys.splice(index,1);
                        historys.unshift(value);
                        console.log(historys)
                        $("#container button").eq(index).remove();
                        $("#container").prepend(`<button>${value}</button>`)
                        $("#input").val("")
                    }
                }
            }
        })
    </script>

数组中被搜索的值,置于顶部

<body>
    <input type="text" id="app">
    <script>
        /* 让数组中被搜索的值,至于顶部 */
        var arr= ["html","css","js","vue","react"]
        /* ["js","html","css","vue","react"] */
        $("#app").keyup( function(event){
            if(event.keyCode==13){
                var value = $(this).val();
                if(arr.indexOf(value)!=-1){
                    console.log(value)
                    var index = arr.indexOf(value);
                    arr.splice(index,1);
                    arr.unshift(value);
                    console.log(arr);
                }
            }
        })
    </script>
</body>

将一个数组分成多个数组

<body>
    <script>
        /* 查询
        slice()
        indexOf
        includes
         */
        var arr = [2,3,4,5,9];
                /* 0  1 2 3 4 */
        var res = arr.slice(1,4);
        console.log(res);
        console.log(arr);

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

网易云

<body>
    <div class="container">

    </div>
   <script>
       /* 1、将远程的数据获取过来 */
       var url = "http://47.108.197.28:3000/top/playlist?limit=35"
       $.ajax({
           url,
           success:res=>{
               var playlists = res.playlists;
               console.log(playlists)
               playlists.forEach(item=>{
                   var {name,coverImgUrl} = item;
                   console.log(name,coverImgUrl)
                   var template = `
                   <div class="item">
                     <img src="${coverImgUrl}" />
                    <p>${name}</p>
                   </div>
                   `
                   $(".container").append(template);

               })
           }
       })
   </script>
</body>

得到[9,33,50] 9-33-50

 var arr = [[2,3,4],[10,11,12],[20,10,20]];
        var list=[]
        for(var i=0;i<arr.length;i++){
            console.log(arr[i])
            var sum = 0;
            arr[i].forEach((item,index)=>{
                sum+=item;
            })
            //console.log(sum)
            list.push(sum);

        }
        //console.log(list)
        console.log(list.join("-"))

需要单独取出数组的第一项,

var lists = [["没有","唯一","爱"],["vue","react","angular"],["没有","唯一","爱"]]
            //var newArr = [
                //["没有","vue","java"],
                //["唯一","react","python"]
                //["爱","angular",ruby]
                //...
           // ]
           var sum = [];
           for(var i=0;i<lists.length;i++){
               sum.push([])
           }
           console.log(sum);
           for(var k=0;k<lists.length;k++){
               console.log(k)
               for(j=0;j<lists[k].length;j++){
                   console.log(lists[k][j]);
                   sum[j].push(lists[k][j])
               }
           }

//没有    唯一    爱