1 回答
TA贡献1810条经验 获得超4个赞
尝试这个:
let arr =
[{MOVINGSTATE: "s",XPOINT:53.9172866,YPOINT: 26.518275},
{MOVINGSTATE: "s",XPOINT: 53.9172866,YPOINT: 26.518275},
{MOVINGSTATE: "m",XPOINT: 54.0215383,YPOINT: 26.5275599},
{MOVINGSTATE: "m",XPOINT: 54.0102666,YPOINT: 26.4989583},
{MOVINGSTATE: "s",XPOINT: 54.0016599,YPOINT: 26.5478316}];
let result = arr.reduce( (accumulator, cur) => {
const key = cur["MOVINGSTATE"];
const keyExists = accumulator.find(obj => obj.key === key);
if (!keyExists) {
accumulator.push({key,value:[cur]});
return accumulator;
} else if(accumulator[accumulator.length -1] && accumulator[accumulator.length -1].key === key){
accumulator[accumulator.length -1].value.push(cur);
return accumulator;
} else {
accumulator.push({key,value:[cur]});
return accumulator;
}
}, [])
.map(obj => obj.value)
添加回答
举报