skip to Main Content

Hi i am working on angular15, here i have got json data like this and i have to bring data for address along with profileName, so my response should be like:

Expected Result:

[
{name:'Pharmarx Pharmaceutical Inc', address:'21441 Osborne St Ste C & D, Canoga Park, CA, 91304'},
{name:'Pharmarx Pharmaceutical Inc', address:'441 abc address, Canoga Park, CA, 91304'},
{name:'Pharmarx Pharmaceutical Inc', address:'231 osborne, Canoga Park, CA, 91304'},
{name:'Pharmarx Pharmaceutical Inc', address:'987 osborneOsborne St Ste C & D, Canoga Park, CA, 91304'}, 
{name:'ENCINO PHARMACY_AKA MDR PHARMACEUTICAL CARE', address:'17071 VENTURA BLVD STE 100 ECINO, MI,91316'}
]

Json:

{
  "took": 178,
  "timed_out": false,
  "_shards": {
    "total": 0,
    "successful": 0,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 10000,
      "relation": "gte"
    },
    "max_score": 12.9226,
    "hits": [
      {
        "_index": "pulse",
        "_id": "17320",
        "_score": 12.9226,
        "_source": {
          "ProfileName": "Pharmarx Pharmaceutical Inc",
          "BusinessModel": "Dispenser",
          "LicenseInfo": [
            {
              "LicenseType": "Resident Licensed Sterile Compounding",
              "LicenseNumber": "99658",
              "AddressLine1": "21441 Osborne St Ste C & D",
              "AddressLine2": "441 abc address",
              "AddressLine3": "231 osborne",
              "AddressLine4": "",
              "Zip": "91304",
              "City": "Canoga Park",
              "State": "CA",
            },
            {
              "LicenseType": "Retail Pharmacy",
              "LicenseNumber": "50511",
              "AddressLine1": "21441 Osborne St Ste C & D",
              "AddressLine2": "987 osborne",
              "AddressLine3": "",
              "AddressLine4": "",
              "Zip": "91304",
              "City": "Canoga Park"
            }
          ]
        }
      },
      {
        "_index": "pulse",
        "_id": "7723",
        "_score": 10.891928,
        "_source": {
          "ProfileName": "ENCINO PHARMACY_AKA MDR PHARMACEUTICAL CARE",
          "State": "MI",
          "BusinessModel": "Dispenser",
          "LicenseInfo": [
            {
              "LicenseType": "PHARMACY",
              "LicenseNumber": "5301010993",
              "AddressLine1": "17071 VENTURA BLVD STE 100",
              "AddressLine2": "",
              "AddressLine3": "",
              "AddressLine4": "",
              "Zip": "91316",
              "City": "ENCINO"
            }
          ]
        }
      },
      {
        "_index": "pulse",
        "_id": "7724",
        "_score": 9.796367,
        "_source": {
          "ProfileName": "Encino Pharmacy_aka Mdr Pharmaceutical Care",
          "State": "CA",
          "BusinessModel": "Dispenser",
          "LicenseInfo": [
            {
              "LicenseType": "Retail Pharmacy",
              "LicenseNumber": "34818",
              "AddressLine1": "17059-17071 VENTURA BLVD.",
              "AddressLine2": "STE. 100",
              "AddressLine3": "",
              "AddressLine4": "",
              "Zip": "91316",
              "City": "ENCINO"
            }
          ]
        }
      },
      {
        "_index": "pulse",
        "_id": "192",
        "_score": 9.656487,
        "_source": {
          "ProfileName": "Abdin Pharmacies Pharma LLC",
          "State": "FL",
          "BusinessModel": "Dispenser",
          "LicenseInfo": [
            {
              "LicenseType": "Pharmacy",
              "LicenseNumber": "036936",
              "AddressLine1": "1873 SECOND AVE. NEW",
              "AddressLine2": "",
              "AddressLine3": "",
              "AddressLine4": "",
              "Zip": "10029",
              "City": "YORK"
            },
            {
              "LicenseType": "Pharmacy",
              "LicenseNumber": "033404",
              "AddressLine1": "1401 BRONX RIVER AVE.",
              "AddressLine2": "",
              "AddressLine3": "",
              "AddressLine4": "",
              "Zip": "10472",
              "City": "BRONX"
            },
            {
              "LicenseType": "Pharmacy",
              "LicenseNumber": "PH33043",
              "AddressLine1": "32866 US Hwy 19N",
              "AddressLine2": "",
              "AddressLine3": "",
              "AddressLine4": "",
              "Zip": "34684",
              "City": "PALM HARBOR"
            }
          ]
        }
      },
      {
        "_index": "pulse",
        "_id": "17304",
        "_score": 9.186218,
        "_source": {
          "ProfileName": "PHARMALABS, LLC PharmaLabs",
          "State": "FL",
          "BusinessModel": "Dispenser",
          "LicenseInfo": [
            {
              "LicenseType": "Pharmacy",
              "LicenseNumber": "PH26495",
              "AddressLine1": "10901 ROOSEVELT BOULEVARD NORTH SUITE 1200",
              "AddressLine2": "",
              "AddressLine3": "",
              "AddressLine4": "",
              "Zip": "33716",
              "City": "Saint Petersburg"
            }
          ]
        }
      }
    ]
  }
}

Ts:

 results = searchData.hits;
          if (results.hits.length > 0) {
            results.hits.forEach(h => {
              let item = h._source;
              item.LicenseInfo.forEach(ap => {
                let address = `${ap.AddressLine1} ${ap.AddressLine2} ${ap.AddressLine3} ${ap.AddressLine4} ${ap.City} ${item.State} ${ap.Zip}`;
                let addresses = { name: item.ProfileName, address }
                data = data.concat(addresses);
              })
              this.seeResultsData = data;
              return data;
            })
            return null;
          }

now, i am getting this.seeResultsData as
Currently i am getting:

[
{name:'Pharmarx Pharmaceutical Inc', address:'21441 Osborne St Ste C & D 441 abc address 231 osborne 987 osborneOsborne St Ste C & D, Canoga Park, CA, 91304'},
{name:'ENCINO PHARMACY_AKA MDR PHARMACEUTICAL CARE', address:'17071 VENTURA BLVD STE 100 ECINO, MI,91316'}
]

2

Answers


  1. In order to get all the adresses, you could try something like this:

    const result:{ name:string, adress:string }[] = [];
    searchData.hits.hits.foreach(hit=>{
     const source = hit._source;
     const name = source.ProfileName;
     source.LicenseInfo.foreach(license=>{
       const adresseKeys = Object.keys(licence).filter(key => key.startsWith('AddressLine'));
       adresseKeys.foreach(key=>{
         if(!!licence[key]){ 
           const adress = `${licence[key]}, ${licence.City}, ${source.State}, ${licence.Zip}`
           result.push({name, adress}) }
       })
     })
    })
    
    this.seeResultsData = result;
    
    Login or Signup to reply.
  2. this.source.hits.hits.forEach((obj: any) => {
      obj._source.LicenseInfo.forEach((subObj:any) => {
        this.address.push({
          name: obj._source.ProfileName,
          address: `${subObj.AddressLine1}, ${subObj.AddressLine2}, ${subObj.AddressLine3}, ${subObj.AddressLine4}, ${subObj.City}, ${subObj.State}, ${subObj.Zip}`
        })
      });
    });
    

    output

    [
      {
        "name": "Pharmarx Pharmaceutical Inc",
        "address": "21441 Osborne St Ste C & D, 441 abc address, 231 osborne, , Canoga Park, CA, 91304"
      },
      {
        "name": "Pharmarx Pharmaceutical Inc",
        "address": "21441 Osborne St Ste C & D, 987 osborne, , , Canoga Park, undefined, 91304"
      },
      {
        "name": "ENCINO PHARMACY_AKA MDR PHARMACEUTICAL CARE",
        "address": "17071 VENTURA BLVD STE 100, , , , ENCINO, undefined, 91316"
      },
      {
        "name": "Encino Pharmacy_aka Mdr Pharmaceutical Care",
        "address": "17059-17071 VENTURA BLVD., STE. 100, , , ENCINO, undefined, 91316"
      },
      {
        "name": "Abdin Pharmacies Pharma LLC",
        "address": "1873 SECOND AVE. NEW, , , , YORK, undefined, 10029"
      },
      {
        "name": "Abdin Pharmacies Pharma LLC",
        "address": "1401 BRONX RIVER AVE., , , , BRONX, undefined, 10472"
      },
      {
        "name": "Abdin Pharmacies Pharma LLC",
        "address": "32866 US Hwy 19N, , , , PALM HARBOR, undefined, 34684"
      },
      {
        "name": "PHARMALABS, LLC PharmaLabs",
        "address": "10901 ROOSEVELT BOULEVARD NORTH SUITE 1200, , , , Saint Petersburg, undefined, 33716"
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search