VUE对json数组中某个字段相同的数据添加到同个数组
效果图:代码:mounted() {this.CS();},methods: {CS() {var data = [{"name": "C","size": 240,"data": 57},
·
效果图:
代码:
mounted() {
this.CS();
},
methods: {
CS() {
var data = [{
"name": "C",
"size": 240,
"data": 57
},
{
"name": "F",
"size": 520,
"data": 42
},
{
"name": "G",
"size": 240,
"data": 47
},
{
"name": "C",
"size": 520,
"data": 23
},
{
"name": "F",
"size": 240,
"data": 14
},
{
"name": "G",
"size": 520,
"data": 62
},
{
"name": "C",
"size": 240,
"data": 34
},
{
"name": "F",
"size": 520,
"data": 25
},
{
"name": "G",
"size": 240,
"data": 35
}
]
console.log(data)
var echararr = [];
for (var i = 0; i < data.length; i++) {
var arrindex=echararr.findIndex((item, index, arr) => {return item.name === data[i].name})//获取下标
if(arrindex==-1){//不存在盘符数据的添加盘符数据
echararr.push({
"name": data[i].name,
"size": data[i].size,
"data": [data[i].data]
});
}else{//有盘符则往盘符数据中添加
echararr[arrindex].data.push(data[i].data)
}
//console.log(arrindex);
}
console.log(echararr)
}
}```
更多推荐
已为社区贡献1条内容
所有评论(0)