var strArray = [
{
key: 1, name: 'b01', typeName: '水平', data: '10.0'
}, {
key: 2, name: 'b01', typeName: '竖直', data: '10.50'
}, {
key: 3, name: 'b01', typeName: '斜角', data: '8.2'
},
{
key: 4, name: 'b02', typeName: '水平', data: '10.0'
}, {
key: 5, name: 'b03', typeName: '水平', data: '10.50'
}, {
key: 6, name: 'b02', typeName: '竖直', data: '8.2'
}
]
var map = {},
dest = [];
for (var i = 0; i < strArray.length; i++) {
var ai = strArray[i];
if (!map[ai.name]) { //依赖分组字段可自行更改!
dest.push({
name: ai.name, //依赖分组字段可自行更改!
data: [ai.data],
typeName: [ai.typeName]
});
map[ai.name] = ai; //依赖分组字段可自行更改!
} else {
for (var j = 0; j < dest.length; j++) {
var dj = dest[j];
if (dj.name == ai.name) { //依赖分组字段可自行更改!
dj.data.push(ai.data);
dj.typeName.push(ai.typeName);
break;
}
}
}
}
console.log(dest);
dest.forEach(_val => {
console.log(_val)
console.log(_val.name)
console.log(_val.data)
_val[_val.name] = _val.data
})
console.log(dest);