1.todolist
<body>
<input type="text" id="input">
<h2>历史搜索</h2>
<div id="app">
</div>
<script>
var input = document.getElementById("input");
var app = document.getElementById("app");
var historys = []
/* 1、获取输入框的值 */
input.onkeydown = function (event) {
if (event.keyCode == 13) {
// console.log(this.value)
/* 2、只要数组中没有的值同时字符不能空,才向数组添加 */
if (this.value != "" && historys.indexOf(this.value) == -1) {
var p = document.createElement("p");
p.innerHTML = this.value;
app.prepend(p);
historys.unshift(this.value);
this.value = ""
console.log(historys)
}else{
alert("输入的字段不能是空字符或相同的字段")
}
}
}
</script>
</body>
2.求数组的和
<script>
var arr = [1,2,3,4,5,8]
var sum = 0;
for(var i = 0;i<arr.length;i++){
sum +=arr[i];
}
console.log(sum);
</script>
3.大于16岁组成一个新数组
<script>
// age >16
var arr = [
{name:"李四" , age:"18岁"},
{name:"李四" , age:"20岁"},
{name:"李四" , age:"8岁"},
{name:"李四" , age:"16岁"},
]
var test = [];
for(var i = 0; i<arr.length; i++){
var age = parseInt(arr[i].age);
if(age>16){
console.log(arr[i]);
test.push(arr[i]);
}
}
console.log(test);
</script>
4.一个数组分成三个数组
<script>
var arr = [1,2,3,4,5,6,7,8,9]
var test = [];
for(var i = 0; i<arr.length; i+=3){
test.push(arr.slice(i,i+3));
}
console.log(test);
</script>
5.求最小值
<script>
var arr = [4,6,18,1];
var min = arr[0];
for(var i=1; i<arr.length; i++){
if(min>arr[i]){
min = arr[i];
}
}
console.log(min);
</script>
6.sort
6.1
<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>
6.2
<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>
7.对象的某个属性是变量,不能使用. 只能使用[]
<script>
//对象的某个属性是变量,不能使用. 只能使用[]
var obj = {
name:"lisi",
age:18
}
var value = "name";
console.log(obj[value]);
</script>
7.1 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>
8.join 例子
8.1 吴京/章子怡/徐峥
<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>
8.2 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>
9.forEach
9.1
<script>
var arr = [2,3,4];
var newArr = [];
arr.forEach(item=>{
console.log(item)
newArr.push(item*2);
})
console.log(newArr)
</script>
9.2
<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>
10. map
<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>
11. filter
11.1 大于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>
11.2 输入李
<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>
12. 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>
13. 历史搜索
<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>
14. 数组中被搜索的值,置于顶部
<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>
15. 将一个数组分成多个数组
<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>
16. 网易云
<!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>
<style>
*{
margin: 0;
padding: 0;
}
.container{
width: 730px;
height: 460px;
border: 1px solid rgb(191, 191, 191);
padding: 20px;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.container .item{
width: 140px;
height: 204px;
margin-right: 42px;
overflow: hidden;
position: relative;
/* border: 1px solid #333; */
}
.container .item:nth-child(4n+4){
width: 140px;
margin-right: 0;
}
.item img{
width: 100%;
}
.title{
width: 140px;
height: 27px;
background-color: #333;
position: absolute;
top: 113px;
opacity: 0.6;
color:white;
font-size: 12px;
box-sizing: border-box;
padding-top: 6px;
padding-left: 10px;
overflow: hidden;
}
.icon-24gf-headphones{
float: left;
margin-right: 5px;
}
.title .icon-bofang{
font-size: 18px;
float: right;
}
</style>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<link rel="stylesheet" href="http://at.alicdn.com/t/font_2781855_y0crm7ypji.css">
</head>
<body>
<div class="container">
</div>
<script>
// 1、将远程的数据拿到本地
var url = 'http://47.108.197.28:3000/top/playlist/?limit=8'
$.ajax({
url,
success:res=>{
// console.log(res.playlists);
var playlists = res.playlists
playlists.forEach(item => {
console.log(item);
var {name,coverImgUrl,playCount} = item
var count = Math.floor(playCount/10000)+"万"
//console.log(count);
var template = `
<div class="item">
<img src="${coverImgUrl}" alt="">
<p>${name}</p>
<div class="title">
<i class="iconfont icon-24gf-headphones"></i>
<i class="iconfont icon-bofang"></i>
${count}
</div>
</div>
`
$('.container').append(template)
});
}
})
</script>
</body>
</html>