1

i am using angular15 and have an autocomplete search based on input, but the response i get is having License number as array of strings in all the objects. now i need to collect all license number from objects and make it as one array of object or array of Strings.

Expected output :

LicenseInfo: [array[0].LicenseNumber, array[1].LicenseNumber, ........ array[n].LicenseNumber]

Ts:

for (var item in results.hits) {
            let dataValue = {licenseNumber:''}
            dataValue.licenseNumber= results.hits[item]._source.LicenseNumber;
            data.push(dataValue);
          }

Json:

  {
      "hits": {
        "hits": [
          {
            "_source": {
              "ProfileName": "",
              "AddressLine1": [
                "7508 abc 105",
                "7400 def, Suite 200",
                "727 xyz",
                "8001 123",
                "1650 ngr",
              ],
              "AddressLine2": [
              "11333 East ",
                "2308 North ",
                "8831 East ",
                "204 dsj",
                "1517 HWY "
            ],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "116-900",
                "749-01",
                "77-52",
                "179",
                "702",
                "746",
                "7402",
                "74-5801",
                "741",
                "7342"
              ],
              "City": [
                "OKLAHOMA CITY",
                "Oklahoma City",
                "OKLAHOMA CITY",
                "OKLAHOMA CITY",
                "Broken Arrow",
                "Tulsa",
                "BROKEN ARROW",
                "TULSA",
                "PRYOR",
                "GUYMON"
              ],
              "State": "OK",
              "LicenseType": "Wholesale Distributor",
              "BusinessModel": "Wholesaler",
              "LicenseNumber": [
                "1-W-1",
                "1-W-5",
                "1-W-6",
                "1-W-82",
                "2-W-12",
                "2-W-45",
                "2-W-416",
                "2-W-91",
                "30-W-21",
                "43-W-95"
              ]
            }
          },
          {
            "_source": {
              "ProfileName": "HEALTHCARE ",
              "AddressLine1": [
                "110 Rsahasd"
              ],
              "AddressLine2": [
                "220 road"
              ],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "73075"
              ],
              "City": [
                "PAULS VALLEY"
              ],
              "State": "OK",
              "LicenseType": "Retail",
              "BusinessModel": "Dispenser",
              "LicenseNumber": [
                "2-667"
              ]
            }
          },
          {
            "_index": "pulse",
            "_id": "19297",
            "_score": 5.054507,
            "_source": {
              "ProfileName": "PHARMACY",
              "AddressLine1": [
                "101 West MAIN",
                "101 East MAIN"
              ],
              "AddressLine2": [],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "749",
                "739"
              ],
              "City": [
                "JOHN",
                "JON"
              ],
              "State": "OK",
              "LicenseType": "Retail",
              "BusinessModel": "Dispenser",
              "LicenseNumber": [
                "1-89",
                "1-39"
              ]
            }
          },
          {
            "_index": "pulse",
            "_id": "22739",
            "_score": 5.054507,
            "_source": {
              "ProfileName": "THE MEDICINE ",
              "AddressLine1": [
                "311 N WASHINGTON AVENUE",
                "311 N WASHINGTON AVENUE"
              ],
              "AddressLine2": [],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "74701-3641",
                "74701-3641"
              ],
              "City": [
                "DURANT",
                "DURANT"
              ],
              "State": "OK",
              "LicenseType": "Retail",
              "BusinessModel": "Dispenser",
              "LicenseNumber": [
                "27-4393",
                "27-8581"
              ]
            }
          },
          {
            "_index": "pulse",
            "_id": "27256",
            "_score": 5.054507,
            "_source": {
              "ProfileName": "WAL-MART ",
              "AddressLine1": [
                "7520 East ",
                "772 North",
                "2705 West",
                "1018 N ",
                "3571 W ",
                "2551 CLASS",
                "13503 S ",
                "3651 CLASS",
                "3301 SW 104",
                "4545 W 6TH"
              ],
              "AddressLine2": [
              "6437 North",
                "14185 drive",
                "4900 Sooner",
                "2001 SST",
                "2019 EST",
                "10938 SDR",
                "12200 SWACO",
                "12912 E N",
                "3900 EHILLDR",
                "3139 SE",
                ],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "63110",
                "63132",
                "63020",
                "63135",
                "63018",
                "64137",
                "64133",
                "64033",
                "64055",
                "64014",
              ],
              "City": [
                "MIDWEST CITY",
                "WARR ACRES",
                "CHOCTAW",
                "OKLAHOMA CITY",
                "CHICKASHA",
                "TULSA",
                "TULSA",
                "GLENPOOL",
                "OWASSO",
                "BROKEN ARROW",
              ],
              "State": "OK",
              "LicenseType": "Retail",
              "BusinessModel": "Dispenser",
              "LicenseNumber": [
                "1-5770",
                "1-6379",
                "1-6621",
                "1-6934",
                "18-5060",
                "2-3864",
                "2-5116",
                "2-5298",
                "2-5444",
                "2-5449",
                "2-6632",
                "26-6921",
                "2-6710",
                "2-7368",
                "2-7447",
                "29-3720",
                "30-6922",
                "34-4508",
                "35-5557",
                "3-6891",
                "46-5049",
                "52-5782",
                "55-5459",
                "5-6935",
                "7-6284",
                "7-6951",
                "7-7474",
                "7-7573",
                "7-7692",
                "8-5133"
              ]
            }
          }
        ]
      }
    } 
Bhrungarajni
  • 2,415
  • 11
  • 44
  • 88

2 Answers2

2

Based on this SO you can try something like

var data={
      "hits": {
        "hits": [
          {
            "_source": {
              "ProfileName": "",
              "AddressLine1": [
                "7508 abc 105",
                "7400 def, Suite 200",
                "727 xyz",
                "8001 123",
                "1650 ngr",
              ],
              "AddressLine2": [
              "11333 East ",
                "2308 North ",
                "8831 East ",
                "204 dsj",
                "1517 HWY "
            ],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "116-900",
                "749-01",
                "77-52",
                "179",
                "702",
                "746",
                "7402",
                "74-5801",
                "741",
                "7342"
              ],
              "City": [
                "OKLAHOMA CITY",
                "Oklahoma City",
                "OKLAHOMA CITY",
                "OKLAHOMA CITY",
                "Broken Arrow",
                "Tulsa",
                "BROKEN ARROW",
                "TULSA",
                "PRYOR",
                "GUYMON"
              ],
              "State": "OK",
              "LicenseType": "Wholesale Distributor",
              "BusinessModel": "Wholesaler",
              "LicenseNumber": [
                "1-W-1",
                "1-W-5",
                "1-W-6",
                "1-W-82",
                "2-W-12",
                "2-W-45",
                "2-W-416",
                "2-W-91",
                "30-W-21",
                "43-W-95"
              ]
            }
          },
          {
            "_source": {
              "ProfileName": "HEALTHCARE ",
              "AddressLine1": [
                "110 Rsahasd"
              ],
              "AddressLine2": [
                "220 road"
              ],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "73075"
              ],
              "City": [
                "PAULS VALLEY"
              ],
              "State": "OK",
              "LicenseType": "Retail",
              "BusinessModel": "Dispenser",
              "LicenseNumber": [
                "2-667"
              ]
            }
          },
          {
            "_index": "pulse",
            "_id": "19297",
            "_score": 5.054507,
            "_source": {
              "ProfileName": "PHARMACY",
              "AddressLine1": [
                "101 West MAIN",
                "101 East MAIN"
              ],
              "AddressLine2": [],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "749",
                "739"
              ],
              "City": [
                "JOHN",
                "JON"
              ],
              "State": "OK",
              "LicenseType": "Retail",
              "BusinessModel": "Dispenser",
              "LicenseNumber": [
                "1-89",
                "1-39"
              ]
            }
          },
          {
            "_index": "pulse",
            "_id": "22739",
            "_score": 5.054507,
            "_source": {
              "ProfileName": "THE MEDICINE ",
              "AddressLine1": [
                "311 N WASHINGTON AVENUE",
                "311 N WASHINGTON AVENUE"
              ],
              "AddressLine2": [],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "74701-3641",
                "74701-3641"
              ],
              "City": [
                "DURANT",
                "DURANT"
              ],
              "State": "OK",
              "LicenseType": "Retail",
              "BusinessModel": "Dispenser",
              "LicenseNumber": [
                "27-4393",
                "27-8581"
              ]
            }
          },
          {
            "_index": "pulse",
            "_id": "27256",
            "_score": 5.054507,
            "_source": {
              "ProfileName": "WAL-MART ",
              "AddressLine1": [
                "7520 East ",
                "772 North",
                "2705 West",
                "1018 N ",
                "3571 W ",
                "2551 CLASS",
                "13503 S ",
                "3651 CLASS",
                "3301 SW 104",
                "4545 W 6TH"
              ],
              "AddressLine2": [
              "6437 North",
                "14185 drive",
                "4900 Sooner",
                "2001 SST",
                "2019 EST",
                "10938 SDR",
                "12200 SWACO",
                "12912 E N",
                "3900 EHILLDR",
                "3139 SE",
                ],
              "AddressLine3": [],
              "AddressLine4": [],
              "Zip": [
                "63110",
                "63132",
                "63020",
                "63135",
                "63018",
                "64137",
                "64133",
                "64033",
                "64055",
                "64014",
              ],
              "City": [
                "MIDWEST CITY",
                "WARR ACRES",
                "CHOCTAW",
                "OKLAHOMA CITY",
                "CHICKASHA",
                "TULSA",
                "TULSA",
                "GLENPOOL",
                "OWASSO",
                "BROKEN ARROW",
              ],
              "State": "OK",
              "LicenseType": "Retail",
              "BusinessModel": "Dispenser",
              "LicenseNumber": [
                "1-5770",
                "1-6379",
                "1-6621",
                "1-6934",
                "18-5060",
                "2-3864",
                "2-5116",
                "2-5298",
                "2-5444",
                "2-5449",
                "2-6632",
                "26-6921",
                "2-6710",
                "2-7368",
                "2-7447",
                "29-3720",
                "30-6922",
                "34-4508",
                "35-5557",
                "3-6891",
                "46-5049",
                "52-5782",
                "55-5459",
                "5-6935",
                "7-6284",
                "7-6951",
                "7-7474",
                "7-7573",
                "7-7692",
                "8-5133"
              ]
            }
          }
        ]
      }
    } ;
    
    let retVal=data.hits.hits.map(h=> h._source.LicenseNumber).flat(1);
    console.log(retVal);
jitender
  • 10,238
  • 1
  • 18
  • 44
  • hi Jitender, can you please check this question, https://stackoverflow.com/questions/76217665/how-to-show-parent-child-relation-using-treedata-in-angular15-using-ag-grid – Bhrungarajni May 11 '23 at 03:33
-1
for (var item in results.hits) {
  let tempData = [];
  for (var i = 0; results.hits[item].length; i++) {
    if (
      results &&
      results.hits[item][i] &&
      results.hits[item][i]._source &&
      results.hits[item][i]._source.LicenseNumber
    ) {
      tempData.push(results.hits[item][i]._source.LicenseNumber);
      console.log(tempData);
    }
  }
}
eglease
  • 2,445
  • 11
  • 18
  • 28