1.数组对象(元素为对象)
let arr = [{
type: "Volvo",
year: 2016
}, {
type: "Saab",
year: 2001
}, {
type: "BMW",
year: 2010
}, {
type: "Volvo",
year: 2016
}, {
type: "Saab",
year: 2001
}];
// 数组对象排序
let sarr= arr.sort((a, b) => a.year - b.year);
console.log(sarr);
// 数组对象去重
let Arr = new Set(arr.map(item => JSON.stringify(item)));
// 运用JSON.stringify() //将数组对象转换为数组, 运用Set的特性去重
let arr0 = [...Arr].map(item => JSON.parse(item));
// 运用JSON.parse() //将数组转换为数组对象
console.log(arr0);
2.数组方法(实用)
let arr = [10, 20, 20, 3, 10, 20, 1, 100];
// 在数组末尾添加元素
arr.push(10);
// 翻转数组元素
arr.reverse()
console.log(arr);
// 数组排序 a-b 小-->大,b-a 大-->小
arr.sort((a, b) => a - b);
console.log(arr);
// 取数组中的最大值
let max = Math.max.apply(null, arr);
console.log(max); //100
console.log(Math.max(null, ...arr)); //100
// 取数组中的最小值
let min = Math.min.apply(null, arr);
console.log(min); //1
// 数组去重
let newArr = [...new Set(arr)];
console.log(newArr);