<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var arr = [{
name:"肯德基",price:100,distance:100
},
{
name:"必胜客",price:100,distance:500
},
{
name:"华莱士",price:100,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>
</body>
</html>
让数组中被搜索的值,至于顶部
<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>
历史搜素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>
<body>
<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>
</html>
demo
<script>
var obj = {
top250:["你好666","他的666"],
comingSoon:["防抖111","节流111"],
theaters:["放大镜222","防静电222"]
}
// var arr = [
// {name:你好}
// ]
// 1.获取对象中属性的值
var arr = []
for(var key in obj){
console.log(key);
// 2.需要将属性的值整合到一个数组中去
arr.push(...obj[key])
}
var res = []
var reg = /\d/g
// 3.对数组中的每一项的值,过滤
arr.forEach(name=>{
var name = name.replace(reg,"")
res.push({
name
})
})
console.log(res);
</script>
demo2
<script>
var arr = {
A:[{city:"武汉"},{city:"广州"}],
B:[{city:"深圳"},{city:"广州"}],
C:[{city:"上海"},{city:"武汉"}]
}
//var citys = ["武汉","上海","深圳","广州"]
var list = []
for(var key in arr){
list.push(...arr[key])
}
var res = [];
list.forEach(item=>{
var {city} = item
if(!res.includes(city)){
res.push(city)
}
})
console.log(res);
</script>
数组去重
# 方法一
var arr = [1,2,1,4,5,6,4]
var res=[]
arr.forEach(item=>{
if(res.indexOf(item) ==-1){
res.push(item)
}
})
console.log(res)
# 方法二
// set--类数组对象 里面的值不重复
// ... 展开语法也适用于set对象
var s = new Set([2,3,2,1])
console.log(s) // {2,3,1}
var arr = [1,2,1,4,5,6,4]
var s = new Set(arr)
console.log(s) // {1,2,4,5,6}
console.log([...s]) // [1,2,4,5,6]