0

I'm trying to create single dimension json object from multi dimension object.

I tried something like this in Typescript.

export interface JsonInput {
  ALIST: string[][];
  BLIST: string[][];
  CLIST: string[][];
  DLIST: string[][];
}

export interface JsonOutput {
  AID: string[];
  BID: string[];
  CID: string[];
  DID: string[];
}

splitArray(objArray) {

    if (objArray["ALIST"] != undefined && objArray["ALIST"].length > 0) {
      for (var x = 0; x < objArray["ALIST"].length; x++) {
        if (objArray["ALIST"][x].AID != undefined && objArray["ALIST"][x].AID != "")
          this.JsonInput.ALIST = objArray["ALIST"][x].AID;
      }
    }
}

This is JSON which I receive as input parameter.

{
  "ALIST": [
    {
      "AID": "LWRNKSXR---01CAB101B",
      "COMPANY": "A1"
    },
    {
      "AID": "71/EGNC/912947/BGSO/911",
      "COMPANY": "A2"
    }
  ],
  "BLIST": [
    {
      "BID": "E911",
      "COMPANY": "B1"
    },
    {
      "BID": "B11CBSIG576",
      "COMPANY": "B2"
    }
  ],
  "CLIST": [
    {
      "CID": "7107230837",
      "COMPANY": "C1"
    },
    {
      "CID": "219S601761",
      "COMPANY": "C2"
    }
  ],
  "DLIST": [
    {
      "DID": "UVERSE",
      "COMPANY": "D1"
    },
    {
      "DID": "COMPANY",
      "COMPANY": "D2"
    }
  ]
}

And I'm trying to create below mentioned output. I want to send first object as input parameter to micro service and second to bind company dropdownlist.

{
  "AID": ["LWRNKSXR---01CAB101B", "71/EGNC/912947/BGSO/911"],
  "BID": ["E911", "B11CBSIG576"],
  "CLIST": ["7107230837", "219S601761"],
  "DLIST": ["UVERSE", "COMPANY"]
}

{
    "COMPANY": ["A1","A2","B1","B2","C1","C2","D1","D2"]
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pushkar Rathod
  • 353
  • 2
  • 7
  • 26

2 Answers2

0

this function will do what you're looking for... we traverse the appropriate object to fill in the values for corresponding object in the final one-dimensional array

makeCustomObject() {
    this.finalObj = {
      "AID": [],
      "BID": [],
      "CLIST": [],
      "DLIST": [],
      "COMPANY": []
    };
    for (var i = 0; i < this.jsonObject.ALIST.length; i++) {
      this.finalObj.AID.push(this.jsonObject.ALIST[i].AID);
      this.finalObj.COMPANY.push(this.jsonObject.ALIST[i].COMPANY);
    }
    for (var i = 0; i < this.jsonObject.BLIST.length; i++) {
      this.finalObj.BID.push(this.jsonObject.BLIST[i].BID);
      this.finalObj.COMPANY.push(this.jsonObject.BLIST[i].COMPANY);
    }
    for (var i = 0; i < this.jsonObject.CLIST.length; i++) {
      this.finalObj.CLIST.push(this.jsonObject.CLIST[i].CID);
      this.finalObj.COMPANY.push(this.jsonObject.CLIST[i].COMPANY);
    }
    for (var i = 0; i < this.jsonObject.DLIST.length; i++) {
      this.finalObj.DLIST.push(this.jsonObject.DLIST[i].DID);
      this.finalObj.COMPANY.push(this.jsonObject.DLIST[i].COMPANY);
    }
  }

working stackblitz here

Akber Iqbal
  • 14,487
  • 12
  • 48
  • 70
0

Try a reduce if you can't get flatMap working.

const data = {
  "ALIST": [
    {
      "AID": "LWRNKSXR---01CAB101B",
      "COMPANY": "A1"
    },
    {
      "AID": "71/EGNC/912947/BGSO/911",
      "COMPANY": "A2"
    }
  ],
  "BLIST": [
    {
      "BID": "E911",
      "COMPANY": "B1"
    },
    {
      "BID": "B11CBSIG576",
      "COMPANY": "B2"
    }
  ],
  "CLIST": [
    {
      "CID": "7107230837",
      "COMPANY": "C1"
    },
    {
      "CID": "219S601761",
      "COMPANY": "C2"
    }
  ],
  "DLIST": [
    {
      "DID": "UVERSE",
      "COMPANY": "D1"
    },
    {
      "DID": "COMPANY",
      "COMPANY": "D2"
    }
  ]
};


const process = data => Object.values(data).reduce((result, list) => list.reduce((result, item) => {
  const key = Object.keys(item)[0];
  if (result.data[key]) {
    result.data[key].push(item[key]);
  } else {
    result.data[key] = [item[key]];
  }
  result.company.push(item.COMPANY);
  return result;
}, result), { data: {}, company: [] });

console.log(process(data));
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
  • Thanks Adrian for providing example but this will work with java script code only. In typescript page when I integrate your code, it throws error for flatmap. – Pushkar Rathod Jul 02 '19 at 06:03
  • https://stackoverflow.com/questions/53556409/typescript-flatmap-flat-flatten-doesnt-exist-on-type-any – Adrian Brand Jul 02 '19 at 06:24