1.sort
sort排序修改原数组
1.1普通数组排序
let arr = [8, 9, 5, 4, 1, 2, 3];
//sort排序后不会生成新的数组,直接修改原数组
arr.sort((a, b) => {
//升序用 a-b
// return a-b
//降序用 b-a
return b - a
})
1.2对象数组排序
let heros = [{
id: '101',
name: '艾格特',
power: 100000
},
{
id: '102',
name: '麦迪文',
power: 130000
},
{
id: '103',
name: '古尔丹',
power: 90000
},
{
id: '104',
name: '萨尔',
power: 50000
},
]
heros.sort((a, b) => {
//升序用 a-b
// return a.power - b.power
//降序用 b-a
return b.power - a.power
})
2.filter
不会改变原数组,创建一个新的数组
2.1普通数组过滤
let arr = [8, 9, 5, 4, 1, 2, 3];
let filterNumber = 5;
const newArr = arr.filter((number) => {
return number>filterNumber
});
console.log(newArr)
2.2对象数组过滤
let heros= [{
id: '101',
name: '艾格特',
power: 100000
},
{
id: '102',
name: '麦迪文',
power: 130000
},
{
id: '103',
name: '古尔丹',
power: 90000
},
{
id: '104',
name: '萨尔',
power: 50000
},
]
let filterText = '尔';
const newHeros = heros.filter((hero) => {
return hero.name.indexOf(filterText) != -1
});
console.log(newHeros)
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net