1、获取对象的键值
let obj={
name:'张三',
age:'18'
}
console.log(obj.name);//张三
console.log(Object.keys(obj)[0]);//name
console.log('name' in obj);//true
###获取对象的属性值:obj.name
###获取对象的键:Object.keys(对象名)[下标]
###判断某属性是否存在该对象中:‘属性名’ in ‘对象名’
2、对数组对象进行排序
let arr=[
{name:'小明',age:'40',height:'175'},
{name:'小红',age:'25',height:'168'},
{name:'小李',age:'18',height:'180'},
{name:'小王',age:'40',height:'185'},
]
//以对象中的age进行排序
console.log(arr.sort(function (a, b) {
return a.age - b.age;
}));
//以对象中的height进行排序
console.log(arr.sort(function (a, b) {
return a.height - b.height;
}));
//判断:按照age排序,如果age相同,按照height排序。
console.log(arr.sort(function (a, b) {
if (a.age == b.age) {
return a.height - b.height;
} else {
return a.age - b.age;
}
}));