排序
var arr1 = [{ name:"肯德基",price:100,distance:500 },{ name:"必胜客",price:200,distance:100 },{ name:"华莱士",price:50,distance:1000 }] var mySelect = document.getElementById("mySelect") mySelect.onchange = function(){ // if(this.value === "distance"){ // arr1.sort(function(a,b){ // return a.distance-b.distance // }) // console.log(arr1); // }else{ // arr1.sort(function(a,b){ // return a.price-b.price // }) // console.log(arr1); // } var value = this.value // 对象的某个属性时变量,不能用. 只能使用[]的方式 arr1.sort((a,b)=>a[value]-b[value]) console.log(arr1); }
var sum = 0 var arr = [{ name:"肯德基",price:100,distance:500,num:2 },{ name:"必胜客",price:200,distance:100,num:1 },{ name:"华莱士",price:50,distance:1000,num:5 }] for(var i=0;i<arr.length;i++){ sum += (arr[i].price*arr[i].num) } console.log(sum);求总价格
遍历
var value var arr = [ {name:'html',value:"12px"}, {name:'css',value:"13px"}, {name:'hjs',value:"14px"} ] arr.forEach(item=>{ value = parseInt(item.value)*2; item.value = value }) console.log(arr);
var nums = [] var arr = [ {name:'html',value:"12px"}, {name:'css',value:"13px"}, {name:'hjs',value:"14px"} ] arr.map(item=>{ nums.push(parseInt(item.value)*2) }) console.log(nums.join("-")); var str = [12,13,14] for(let v in str){ str[v] *= 2 } console.log(str);将数组中的value值变成24-26-28
some
var arr = [3,4,5,6] // 只要数组中一项满足某个条件结果就是true var res = arr.some(item=>{ return item>7 }) console.log(res); //false
every
// 返回值为布尔值 // every 数组中的每一个项,都必须满足某个条件 var arr = [2,3,4] var res = arr.every(item=>{ return item>2 }) console.log(res); //false
filter
var students = [ {name:"李四",age:"18岁"}, {name:"李二",age:"11岁"}, {name:"李五",age:"19岁"}, {name:"王四",age:"22.5岁"} ] var result = students.filter(item=>{ return parseInt(item.age)>11 }) console.log(result);拿到age大于11岁的
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 var res = students.filter(item=>{ return item.name.includes(keyword) || item.age.includes(keyword) }) console.log(res); } } 在输入框中输入值,返回符合条件的新数组
find
var arr = [2,3,4,3] var res = arr.find(item=>{ return item == 3 }) console.log(res);//3
findIndex
var arr = [2,3,4] var index = arr.findIndex(item=>{ return item == 4 }) console.log(index); //2
<input type="text" id="app"> <script> var arr = [ {name:"李四",id:1001}, {name:"王四",id:1002}, {name:"张四",id:1003}, {name:"赵四",id:1004} ] var app = document.getElementById('app') app.onkeydown = function(event){ if(event.keyCode === 13){ // var keyword = this.value // for(var key in arr){ // if(keyword == arr[key].id){ // arr.splice(key,1) // console.log(arr); // } // } var id = Number(this.value) var res = arr.findIndex(item=>{ return item.id == id }) arr.splice(res,1) console.log(arr); } } </script> 在输入框中输入id,通过输入的id在数组中查找,有的话,就删除
复习
<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() //tips:jQuery可以将带html结构的字符串,转化为html var template = ` <button>${value}</button> ` // 只有数组中不包含输入的关键字才向数组添加 if(value && !historys.includes(value)){ historys.unshift(value) console.log(historys); // 3、渲染数据到页面 $('#container').prepend(template) $(this).val("") }else{ // 4、当输入的值数组中存在的时候,让其置于数组的首位 var index = historys.indexOf(value) var res = historys.splice(index,1) //historys.unshift(value) historys.unshift(...res) console.log(historys); // ui联动 $('#container button').eq(index).remove() $('#container').prepend(template) $(this).val("") } } }) </script> 历史搜索:在输入框中输入,然后将其value值添加到数组中,并通过字符串模版将其添加到页面上, 如果输入的有重复的值,则将那个中放到数组和页面的顶部(前面)
让数组中被搜索的值置于顶部 <input type="text" id="app"> <script> // 让数组中被搜索的值置于顶部 var arr = ['html','css','js','vue','react'] $('input').keyup(function(event){ if(event.keyCode === 13){ var value = $(this).val() if(arr.includes(value)){ var index = arr.indexOf(value) var res = arr.splice(index,1) arr.unshift(...res) console.log(arr); $(this).val("") } } }) </script>