• 37、将相同字段属性值放在同一个数组中,并去重对象数组


                const data = [{
                        featureName: "age",
                        bucket: "0-10",
                        iv: 100
                    },
                    {
                        featureName: "gender",
                        bucket: "男",
                        iv: 200
                    },
                    {
                        featureName: "city",
                        bucket: "安徽",
                        iv: 300
                    }, {
                        featureName: "age",
                        bucket: "20-30",
                        iv: 110
                    }, {
                        featureName: "city",
                        bucket: "北京",
                        iv: 310
                    }
                ];
                // 将相同字段featureName的值放到同一数组中
                function mergeObject(data) {
                    var arrayFilted = [];
                    data.forEach(function(value, key) {
                        if(arrayFilted.length == 0) {
                            arrayFilted.push({
                                featureName: value.featureName,
                                bucket: [value.bucket],
                                iv: [value.iv]
                            });
                        } else {
                            arrayFilted.forEach(function(item, index) {
                                if(item.featureName && item.featureName !== value.featureName) {
                                    arrayFilted.push({
                                        featureName: value.featureName,
                                        bucket: [value.bucket],
                                        iv: [value.iv]
                                    });
                                } else if(item.featureName && item.featureName === value.featureName) {
                                    item.bucket.push(value.bucket)
                                    item.iv.push(value.iv)
                                }
                            });
                        }
                    });
                    return arrayFilted;
                }
    
                //去重
                function Es5duplicate(arr, type) {
                    var newArr = [];
                    var tArr = [];
                    if(arr.length == 0) {
                        return arr;
                    } else {
                        if(type) {
                            for(var i = 0; i < arr.length; i++) {
                                if(!tArr[arr[i][type]]) {
                                    newArr.push(arr[i]);
                                    tArr[arr[i][type]] = true;
                                }
                            }
                            return newArr;
                        } else {
                            for(var i = 0; i < arr.length; i++) {
                                if(!tArr[arr[i]]) {
                                    newArr.push(arr[i]);
                                    tArr[arr[i]] = true;
                                }
                            }
                            return newArr;
                        }
                    }
                }
    
                const getData = mergeObject(data);
                console.log(Es5duplicate(getData, 'featureName'));

    最终结果:

  • 相关阅读:
    ios xib或storyBoard的那些小方法
    ios pod库更新到1.0或1.0.1之正确修改podfile文件
    ios UILabel在storyBoard或xib中如何在每行文字不显示完就换行
    ios NSThred多线程简单使用
    Xcode升级插件失效,与添加插件不小心点击Skip Bundle解决办法
    ios app打ipa包
    极光推送碰到的问题
    ios 更新约束
    ios 缺少合规证明
    Path Sum II
  • 原文地址:https://www.cnblogs.com/xlfdqf/p/11454614.html
Copyright © 2020-2023  润新知