• 解决js数组(key去重)中,key相同value不同,将value放在同一个可以中


    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]});
            }
    改为数组对象才能进行传输
     
    踩过这个坑,还有下一个坑等着你,这一路就是给自己填坑,坑填多了,也就习惯了,直到这一路平坦了,也就无怨无悔了。
  • 相关阅读:
    The Tamworth Two chapter 2.4
    USACO Controlling Companies chapter 2.3 已跪
    非递归快排
    链表二路归并
    Money Systems chapter 2.3 dp
    #pragma pack与sizeof union
    快慢指针
    12
    11
    10
  • 原文地址:https://www.cnblogs.com/xiaofeilin/p/13913632.html
Copyright © 2020-2023  润新知