用 splice函数分别实现 push、pop、shift、un
2019-05-10 本文已影响0人
清苑折纸
实现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)