对象数组分类并转换为二维数组或对象数组

19 篇文章 2 订阅

一、问题

 对对象数组按一定标准分类

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

/*希望对你有帮助!

如有错误,欢迎指正,谢谢!

*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值