1、价格升序,距离降序
var arr = [{
name:"肯德基",price:100,distance:100
},
{
name:"必胜客",price:200,distance:500
},
{
name:"华莱士",price:50,distance:1000
}
]
2、
var directs = [
{name:"吴京",id:1001},
{name:"章子怡",id:1002},
{name:"徐峥",id:1003}
]
/* str =吴京/章子怡/徐峥 */
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>
4、es6解构-展开-键值相同
<script>
var arr = [
{
name:"lisi",
age:18,
likes:["lol","足球"],
alikes:["音乐","电影"],
sex:"male"
},
{
name:"王五",
age:17,
likes:["al","栏球"],
alikes:["防抖","放到"],
sex:"male"
},
{
name:"张三",
age:165,
likes:["发顺丰","jf"],
alikes:["音乐9","10电影"],
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)
</script>