selectItems = [
{stores_code:'123',id:'1'},
{stores_code:'123',id:'2'},
{stores_code:'123',id:'3'},
{stores_code:'123',id:'4'},
{stores_code:'567',id:'1'},
{stores_code:'567',id:'2'},
{stores_code:'789',id:'1'},
{stores_code:'789',id:'2'},
];
let tempArr = []; let stores = [];//用于处理有哪些stores_code if(selectItems.length != 0){ for(let key in selectItems){ if(stores.indexOf(selectItems[key]['stores_code']) === -1){ stores.push(selectItems[key]['stores_code']); tempArr[selectItems[key]['stores_code']] = [];//第一次的时候就声明是(二维)数组 tempArr[selectItems[key]['stores_code']].push(selectItems[key]['id']); }else{ tempArr[selectItems[key]['stores_code']].push(selectItems[key]['id']);//同一个stores_code将id写入 } } }
tempArr为最后stores_code去重的值(此时为二维数组)
如果需要将其传输到后台,需要进行改变一下
let data = [];
for(let key in tempArr){
data.push({stores_code:key,id:tempArr[key]});
}
改为数组对象才能进行传输