实现push方法
image.png实现pop方法
image.png实现shift方法
image.png实现unshift方法
image.png对以下代码 users中的对象,分别以 name 字段、age 字段、company 字段进行排序
var users = [
{ name: "John", age: 20, company: "Baidu" },
{ name: "Pete", age: 18, company: "Alibaba" },
{ name: "Ann", age: 19, company: "Tecent" }
]
var users = [
{ name: "John", age: 20, company: "Baidu" },
{ name: "Pete", age: 18, company: "Alibaba" },
{ name: "Ann", age: 19, company: "Tecent" }
]
function compare (identify) {
return function (obj1,obj2) {
if ( obj1[identify] > obj2[identify])
return 1;
else if ( obj1[identify] < obj2[identify])
return -1;
else
return 0;
}
}
users.sort(compare('age'));
console.log(users)
网友评论