Vue 对象数组中 将某个属性的值相同 的对象合并组成新对象数组
原对象数组var list = [{ melonbody: 8.4, strName: '损伤程度', level: '一等品' },{ colorvalue: 8.42, strName: '颜色', level: '一等品' },{ diameter: 8.4, strName: '直径', level: '一等品' },{ melonbody: 6.41, strName: '损伤程度',
·
原对象数组
var list = [
{ melonbody: 8.4, strName: '损伤程度', level: '一等品' },
{ colorvalue: 8.42, strName: '颜色', level: '一等品' },
{ diameter: 8.4, strName: '直径', level: '一等品' },
{ melonbody: 6.41, strName: '损伤程度', level: '二等品' },
{ colorvalue: 6.4, strName: '颜色', level: '二等品' },
{ diameter: 6.42, strName: '直径', level: '二等品' },
{ melonbody: 4.41, strName: '损伤程度', level: '三等品' },
{ colorvalue: 4.41, strName: '颜色', level: '三等品' },
{ diameter: 4.42, strName: '直径', level: '三等品' }
];
目标对象数组
demo = [
{ melonbody: 8.4, level: '一等品', colorvalue: 8.42, diameter: 8.4 },
{ melonbody: 6.41, level: '二等品', colorvalue: 6.4, diameter: 6.42 },
{ melonbody: 4.41, level: '三等品', colorvalue: 4.41, diameter: 4.42 }
];
实现方法:
var list = [
{ melonbody: 8.4, strName: '损伤程度', level: '一等品' },
{ colorvalue: 8.42, strName: '颜色', level: '一等品' },
{ diameter: 8.4, strName: '直径', level: '一等品' },
{ melonbody: 6.41, strName: '损伤程度', level: '二等品' },
{ colorvalue: 6.4, strName: '颜色', level: '二等品' },
{ diameter: 6.42, strName: '直径', level: '二等品' },
{ melonbody: 4.41, strName: '损伤程度', level: '三等品' },
{ colorvalue: 4.41, strName: '颜色', level: '三等品' },
{ diameter: 4.42, strName: '直径', level: '三等品' }
];
let level = '';
let demo = [];
let num = -1;
for (let key of list) {
for (let item in key) {
if (level != key.level) {
level = key.level;
num++;
demo[num] = {};
}
item != 'strName' ? (demo[num][item] = key[item]) : '';
}
}
console.log(demo);
更多推荐
已为社区贡献21条内容
所有评论(0)