js 数据处理 -- 相同属性合并为一个数组
程序员文章站
2022-07-12 21:37:18
...
var yy = [
{
addTime: 100,
data: 0.32,
dataKey: "value",
platformSensorId: "L3_QJ_1",
},
{
addTime: 100,
data: 2.9,
dataKey: "x",
platformSensorId: "L3_QJ_1",
}, {
addTime: 100,
data: 1.9,
dataKey: "y",
platformSensorId: "L3_QJ_2",
},
{
addTime: 200,
data: 0.32,
dataKey: "value",
platformSensorId: "L3_QJ_1",
},
{
addTime: 200,
data: 2.9,
dataKey: "x",
platformSensorId: "L3_QJ_1",
}, {
addTime: 200,
data: 1.9,
dataKey: "y",
platformSensorId: "L3_QJ_2",
}
]
var tyy = [...yy];
console.log(tyy)
function sortArr(arr, str) {
var _arr = [],
_t = [],
_tmp;
arr = arr.sort(function (a, b) {
var s = a[str],
t = b[str];
return s < t ? -1 : 1;
});
if (arr.length) {
_tmp = arr[0][str];
}
for (var i in arr) {
if (arr[i][str] === _tmp) {
_t.push(arr[i]);
// console.log(arr[i])
} else {
_tmp = arr[i][str];
_arr.push(_t);
_t = [arr[i]];
}
}
_arr.push(_t);
return _arr;
}
var arr2 = (sortArr(tyy, 'addTime'))
console.log(arr2)
上一篇: Python的pandas库实战进行一个数据处理的工作
下一篇: 并行数据处理之一:并行流