var arr =[
{
name:"lisi",
age:18,
likes:["lol","足球"],
alikes:["音乐","电影"],
sex:"male"
},
{
name:"王五",
age:17,
likes:["ol","篮球"],
alikes:["函数","事件"],
sex:"male"
},
{
name:"张三",
age:165,
likes:["lo22","足球1"],
alikes:["音乐2","电影3"],
sex:"male"
},
]
var res = [];
for(var i =0;i<arr.length;i++){
// console.log(arr[i])
var name = arr[i].name;
var age = arr[i].age;
var likes = arr[i].likes.concat(arr[i].alikes);
var alikes = arr[i].alikes.concat(arr[i].alikes);
res.push(
{
name:name,
age:age,
likes:likes
}
)
}
console.log(res)
示例2
es6方法
var arr =[
{
name:"lisi",
age:18,
likes:["lol","足球"],
alikes:["音乐","电影"],
sex:"male"
},
{
name:"王五",
age:17,
likes:["ol","篮球"],
alikes:["函数","事件"],
sex:"male"
},
{
name:"张三",
age:165,
likes:["lo22","足球1"],
alikes:["音乐2","电影3"],
sex:"male"
},
]
var res = [];
for(var i =0;i<arr.length;i++){
//解构
var {name,age,likes,alikes} = arr[i];
likes.push(...alikes);
//键和值相同只用写一个
var obj={
name,
age,
likes
}
res.push(obj);
}
console.log(res)
数组中搜索值进行排序,至于顶部
//让数组中被搜索的值,至于顶部
var arr =["html","css","js","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)
}
}
})
字符串请求模板示例
var arr = [
{
imgUrl:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg1.doubanio.com%2Fview%2Fthing_review%2Fl%2Fpublic%2Fp6346199.jpg&refer=http%3A%2F%2Fimg1.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636337497&t=5cf8473b04e7343e539e7d8d4461afbf",
name:"cheng",
age:19
},{
imgUrl:"https://gss0.baidu.com/70cFfyinKgQFm2e88IuM_a/baike/pic/item/0e2442a7d933c8952e6a4f65d81373f08202009c.jpg",
name:"lisi",
age:18
}
]
var res = [];
for(var i=0;i<arr.length;i++){
//console.log(arr[i])
var {imgUrl,name,age} = arr[i];
var template = `
<div class="item">
<p>${name}<p>
<img src="${imgUrl}"/>
<p>${age}</p>
</div>
`
console.log(template)
$("body").prepend(template)
}
获取id下标的值
<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){
//console.log(this.value)
var id = Number(this.value);
//获取id的下标值
//console.log(id);
var index = arr.findIndex(item=>{
return item.id ==id;
})
arr.splice(index,1);
console.log(arr)
}
}
获取键盘输入框的下标数字
<input type="text" id="app">
<script>
var keyword = "李"
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 keyCode = this.value;
console.log(keyword)
var res = students.filter(item=>{
return item.name.includes(keyword)
})
console.log(res)
}
}
遍历方法
//for in
var arr = ["html","css","js"];
for(var key in arr){
console.log(key)//key获取的是数组的下标
//console.log(arr[key])
}
var obj = {
name:"list",
age:18
}
for(var k in obj){
console.log(obj[k])
}
reduce方法
var arr = [{
name:"肯德基",price:100,distance:100,num:2
},
{
name:"必胜客",price:200,distance:50,num:1
},
{
name:"华莱士",price:50,distance:1000,num:5
}
]
var sum = arr.reduce(function(a,b){
console.log(a);
console.log(b);
})
选择示例option
<select id="mySelect">
<option value="price">低价优先</option>
<option value="distance">距离优先</option>
</select>
<script>
var arr = [{
name:"肯德基",price:100,distance:100
},
{
name:"必胜客",price:200,distance:50
},
{
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)
}
根据价格升序排序
var arr = [{
name:"肯德基",price:100,distance:100
},
{
name:"必胜客",price:200,distance:50
},
{
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)
sort方法
var arr = [12,3,5,11,1];
console.log(arr.sort())
var newArr = arr.sort(function(a,b){
return a-b;
})
var c = [12,45,2,3,5];
var xjj = c.sort(function(a,b){
return b-a;
})
console.log(newArr)
console.log(xjj)
join方法连接
<div id="app"></div>
<script>
var dates = ["2019","03","09"]
var str = dates.join("-");
console.log(str)
var app = document.getElementById("app")
app.innerHTML = str;
casts打印在css样式中的方法
<p id="app"></p>
<script>
var directs = [
{name:"吴京",id:1001},
{name:"章子怡",id:1002},
{name:"徐峥",id:1003}
]
var arr = [];
for(var i = 0;i<directs.length;i++){
console.log(directs[i].name)
arr.push(directs[i].name)
}
console.log(arr)
var str = arr.join("/")
console.log(str)
var app = document.getElementById("app");
app.innerHTML = str;