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'));