1-1 数组的方法
var arr = [1,2,3,4,5,7] //[1,2,7]
arr.splice(2,3)
console.log(arr);
var arr = [1,2,3,4,5,7] //[1,8,9,7]
arr.splice(1,4,8,9)
console.log(arr);
1-2 ==运算
// Number(""),Number(" "),Number(null) => 输出 0
console.log(false == ""); // true
console.log(true == 1); // true
console.log(null == undefined); // true
console.log(undefined == false); // false
console.log(1 == "1"); // true
/* 1.只有有一边为 boolean 先两边转为number
2.一边为string,一边为number,先将两遍转为number
// 两边都先转为number
3. null == undefined
4. null 和 undefined 不能转化为其他值
*/
1-3 join 练习
var data ={
country:"美国",
year:1994,
story:"犯罪"
}
// 美国/1994/犯罪
/* 先将对象转为数组 在进行字符串拼接*/
var arr = []
for(var i in data){
arr.push(data[i])
}
var str = arr.join("/")
console.log(str);
1-4 sort 练习
1-4-1 练习1 根据年龄升序、降序
var students = [
{name:"zhang",age:20},
{name:"li",age:18},
{name:"wang",age:9},
{name:"chen",age:14}
]
/*
根据年龄升序
根据年龄降序
*/
var result = students.sort((a,b)=>{
return a["age"]-b["age"]
})
console.log(result);
var result2 = students.sort((a,b)=>{
return b["age"]-a["age"]
})
console.log(result2);
1-4-2 练习2 根据相应的值排序
<button id="like">通过好评数排序</button>
<button id="price">通过价格排序</button>
<script>
var like = document.getElementById("like")
var price = document.getElementById("price")
var sales = [
{name:"张三",like:40,distance:1000,price:10},
{name:"李四",like:60,distance:500,price:50},
{name:"王五",like:30,distance:600,price:40}
]
/* 升序 */
function addOrder(arr,value){
if(Array.isArray(arr)){
var str = arr.sort((a,b)=>{
return a[value]-b[value]
})
return str
}else{
return null
}
}
like.onclick = function(){
console.log(addOrder(sales,"like"));
}
price.onclick = function(){
console.log(addOrder(sales,"price"));
}
</script>
1-4-3 练习3 根据价格升序,距离降序
<!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>
<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>
</html>
1-5 map 练习
var arr = [
{name:"html",value:"12px"},
{name:"css",value:"13px"},
{name:"vue",value:"11px"}
]
arr.map(item =>{
var result = parseInt(item["value"])*2
return item["value"] = result
})
console.log(arr);
/* {name: "html", value: 24}
{name: "css", value: 26}
{name: "vue", value: 22} */
/* var res = []
for(var key in arr){
arr[key].value = parseInt(arr[key].value)*2;
res.push(arr[key])
}
console.log(res);
*/
1-6 filter 练习
var arr = [
{name:"小米",price:1999},
{name:"红米",price:999},
{name:"iphone",price:9999},
{name:"华为",price:3000},
]
var res = arr.filter(item =>{
return item["price"]>1000
})
console.log(res);
1-7 输入框获取对应数据
<input type="text" id="input">
<script>
var arr = [
{name:"你是谁,你在哪",id:1102},
{name:"圈住你",id:1102},
{name:"hello在哪里",id:1102},
{name:"怪你",id:1102},
{name:"world",id:1102}
]
/*
1.enter回车获取关键字
2.使用关键字,对数据查询,得到一个新数组
3.name的字符超过3位以...结尾
*/
/* 1.enter回车获取关键字 */
var input = document.getElementById("input")
input.onkeydown = function(event){
if(event.keyCode == 13){
var value = this.value
/* 2.使用关键字,对数据查询,得到一个新数组 */
if(value){
var res = arr.filter(item =>{
return item["name"].includes(value)
})
/* 3.name的字符超过3位以...结尾 */
res.map(item=>{
if(item.name.length>3){
return item.name=item.name.slice(0,3)+"..."
}
})
console.log(res);
}
}
}
</script>
1-8 分割数组
var arr = [1,2,3,4,5,6,7,8,9]
// [1,2,3] [4,5,6] [7,8,9]
// 0 2
// 3 5
// 6 8
var sum = []
for(var i =0;i<arr.length;i=i+3){
var item = arr.slice(i,i+3)
sum.push(item)
}
console.log(sum);
1-9 数组去重
# 方法一
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]
1-10 输入id,删除数组中对应的项
<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>
1-11 关键词过滤
<body>
<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>
</body>