一、问题
对对象数组按一定标准分类
1.原始对象数组
let arr = [
{ date: '2018-01-06', chinese: '90', math: '109' },
{ date: '2018-01-07', chinese: '110', math: '119' },
{ date: '2018-01-07', chinese: '108', math: '120' },
{ date: '2018-01-08', chinese: '111', math: '108' },
{ date: '2018-01-09', chinese: '110', math: '116' },
{ date: '2018-01-06', chinese: '100', math: '111' },
{ date: '2018-01-07', chinese: '105', math: '103' },
]
2.分类为二维数组
注:此例中按日期date分类(要按其他标准分类,修改判断条件arr_classify[j][i].date === element.date即可)
// 对象数组分类为二维数组
classification_tda = (arr) => {
let arr_classify = [];
//是否放入二维数组
let isPush = false;
arr.map((element, index) => {
//重置标志位
isPush = false;
//第一次直接添加到二维数组中
if (arr_classify.length === 0) {
arr_classify.push([element])
} else {
//遍历查找该类数据是否存在
for (let j = 0; j < arr_classify.length; j++) {
for (let i = 0; i < arr_classify[j].length; i++) {
//分类标准---放入已经存在的类型中
if (arr_classify[j][i].date === element.date) {
arr_classify[j].push(element);
isPush = true;
break;
}
}
}
//分类标准不存在,添加
if (!isPush) {
arr_classify.push([element])
}
}
return arr_classify;
}
let arr_classify = classification_tda(arr);
console.log("arr_classify", arr_classify, arr_classify.length)
//结果
// arr_classify [
// [
// { date: '2018-01-06', chinese: '90', math: '109' },
// { date: '2018-01-06', chinese: '100', math: '111' }
// ],
// [
// { date: '2018-01-07', chinese: '110', math: '119' },
// { date: '2018-01-07', chinese: '108', math: '120' },
// { date: '2018-01-07', chinese: '105', math: '103' }
// ],
// [ { date: '2018-01-08', chinese: '111', math: '108' } ],
// [ { date: '2018-01-09', chinese: '110', math: '116' } ]
// ] 4
3.分类为对象数组
注:和分类为二维数组类似,循环少了一层,标志位通过some方法重置、更新(要按其他标准分类,修改判断条件group.date === element.date即可)
classification_oa = (arr) => {
let arr_classify = [];
arr.map((element, index) => {
if (arr_classify.length === 0) {
arr_classify.push({
date: element.date,
content: [element]
})
} else {
let isPush = arr_classify.some((group, groupKey) => {
if (group.date === element.date) {
arr_classify[groupKey].content.push(element);
return true;
}
})
console.log("isPush", isPush)
if (!isPush) {
arr_classify.push({
date: element.date,
content: [element]
})
}
}
})
return arr_classify;
}
let arr_classify2 = classification_oa(arr);
console.log("arr_classify2", arr_classify2)
//结果
// arr_classify2[
// {
// "date": "2018-01-06",
// "content": [
// {
// "date": "2018-01-06",
// "chinese": "90",
// "math": "109"
// },
// {
// "date": "2018-01-06",
// "chinese": "100",
// "math": "111"
// }
// ]
// },
// {
// "date": "2018-01-07",
// "content": [
// {
// "date": "2018-01-07",
// "chinese": "110",
// "math": "119"
// },
// {
// "date": "2018-01-07",
// "chinese": "108",
// "math": "120"
// },
// {
// "date": "2018-01-07",
// "chinese": "105",
// "math": "103"
// }
// ]
// },
// {
// "date": "2018-01-08",
// "content": [
// {
// "date": "2018-01-08",
// "chinese": "111",
// "math": "108"
// }
// ]
// },
// {
// "date": "2018-01-09",
// "content": [
// {
// "date": "2018-01-09",
// "chinese": "110",
// "math": "116"
// }
// ]
// }
// ]
二、总结
1.写出来感觉也没有那么难了,但是急的时候,还是感觉挺麻烦的,所以记录下来以备不时之需。
2.思路:声明一个变量arr_classify保存结果;遍历原来的数据arr,把每个数据element和arr_classify中的数据对比,已有对应的类别则直接加入,并改变是否加入到arr_classify的标志isPush=true;对比完后还没有加入到arr_classify(isPush-false),则新建一个类别,并且加入到arr_classify
/*希望对你有帮助!
如有错误,欢迎指正,谢谢!
*/