js操作List对象集合常⽤⼩⽅法
1.对List去重,根据对象某个属性去重。
2.对Lisr排序,根据对象某个属性排序。
3.将List对象转为tree结构。
4.对List对象集合,根据id分组,相同的字段累加。
5.对List对象集合过滤。
6.向List集合中添加对象,向数组⾥⾯添加元素。
7.指定⼀个数字,⽣成对应数字长度的数组。
1. 对List去重,根据对象某个属性去重
⽰例
const list = [
{id: 1, name: 'aa' , age: 15},
{id: 2, name: 'bb' , age: 16},
{id: 2, name: 'cc' , age: 17},
];
arrayUnique = (arr, name) => {
const hash = {};
duce((item, next) => {
hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
return item;
}, []);
};
list = this.arrayUnique(list, 'id');
对数组去重
distinct = (a) => {
return Array.from(new Set([...a]))
};
const obj = [1,2,2,3,3,4,5];
console.info('obj', obj);
console.info('distinct', this.distinct(obj));
2. 对Lisr排序,根据对象某个属性排序
⽰例
const list = [
{id: 1, name: 'aa' , age: 15},
{id: 3, name: 'cc' , age: 17},
{id: 2, name: 'bb' , age: 16},
];
compare = (property) => {
return function (a, b) {
const value1 = a[property];
const value2 = b[property];
return value2 - value1;
};
}
list = list.sort(thispare('id'))
倒叙:return value2 - value1;
正序:return value1 - value2;
对数组排序(按字符⽽⾮数值排序)
let arr = [10,0,488,880,13.68];
arr = arr.sort();
console.info('arr', arr);
filter过滤对象数组按值排序
sortRule = (a,b) => {
return a-b;
}
let arr = [10,0,10,880,13.68,130,1220];
arr = arr.sort(this.sortRule);
3. 将List对象转为tree结构
⽰例
const list = [
{id: 1, name: '⼀级' , parentId: 0},
{id: 2, name: '⼆级' , parentId: 1},
{id: 3, name: '三级' , parentId: 2},
];
function buildByRecursive(nodes, rootId = 0) {
const rootMenus = nodes.filter(m => m.parentId === rootId);  rootMenus.forEach((node) => {
findChild(node, nodes);
});
return rootMenus;
}
findChild(node, nodes) {
const {id} = node;
const filters = nodes.filter(m => m.parentId === id);
if (filters.length > 0) {
node.children = filters;
filters.forEach((item) => {
findChild(item, nodes);
});
}
}
4. 对List对象集合,根据id分组,相同的字段累加
⽰例
const list = [
{id: 46, name: "aa", age: 15, startTime: '2019-08-01 00:00:00', endTime: '2019-08-01: 15:23:25'},      {id: 46, name: "aa", age: 15, startTime: '2019-08-02 00:00:00', endTime: '2019-08-02: 15:23:25'},      {id: 46, name: "aa", age: 15, startTime: '2019-08-03 00:00:00', endTime: '2019-08-03: 15:23:25'},      {id: 46, name: "aa", age: 15, startTime: '2019-08-04 00:00:00', endTime: '2019-08-04: 15:23:25'},      {id: 47, name: "bb", age: 18, startTime: '2019-08-13 00:00:00', endTime: '2019-08-13: 15:23:25'},      {id: 47, name: "bb", age: 18, startTime: '2019-08-14 00:00:00', endTime: '2019-08-14: 15:23:25'},    ];
getUpdateArray = (list) => {
const obj = _.groupBy(list, (d)=>d.id);
for (const a in obj) {
if (obj[a].length > 1) {
const ar = [];
var o = {};
let _i = 0;
obj[a].forEach(d=>{
for (const item in d) {
if (o[item] !== d[item]) {
if (o[item]) {
o[item + (_i)] = d[item];
} else {
o[item] = d[item];
}
}
}
_i++;
}
);
obj[a] = [o];
}
}
return _.concat.apply([], Object.values(obj));
};
5. 对List对象集合过滤,filter()函数相关。
删除集合中id=1的对象
const list = [
{id: 1, name: 'aa' , age: 15},
{id: 2, name: 'bb' , age: 16},
{id: 3, name: 'cc' , age: 17},
];
const filterList = list.filter(val => val.id !== 1);
取出集合中id=1的对象
const filterList = list.filter(val => val.id === 1);
6.向List集合中添加对象,push()函数相关。
let list = [
{id: 1, name: 'aa' , age: 15},
{id: 2, name: 'bb' , age: 16},
{id: 3, name: 'cc' , age: 17},
];
list = list.push({
id: 4,
name: 'dd',
age: 18,
});
向数组⾥⾯添加元素,concat()函数相关。
const obj = ['a','b','c'];
const obj2 = at('d');

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。