js vue 数组对象去重
数据结构:
方法一:
const resA = new Map();
this.bigRegionZList = data.filter((arr) => !resA.has(arr.id) && resA.set(arr.id, 1));
方法二:
this.personList = data.filter((item, index, self) => {
const i = self.findIndex(t => t.id === item.id);
return i === index;
})
数组排序
//调用
//纯数组数组调用
this.sortByKey([...new Set(floor)], 0)
//对象数组调用,room为要排序的对象字段
this.sortByKey(arr.floorData, 'room')
//封装方法:
sortByKey(array, key) {
if (key !== 0) {
return array.sort(function (a, b) {
var y = Number(a[key]);
var x = Number(b[key]);
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
})
} else {
return array.sort(function (a, b) {
var y = Number(a);
var x = Number(b);
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
})
}
},