Skip to content
Advertisement

How can I get all Id in array of object by using foreach and merge the all data corresponding ID?

I am working on typescript api i merged all the data with this api i am getting some data from another api function merged with this api, I will give my some code only data adding first index of array. I need to “Communes” and “Category” and “Location” data add all issuer_id this data i will show below.

This code i am using here for merge data.

 let premiumValue = (Object.values(values))
        console.log(premiumValue)      
    const issuerId = Object.values(premiumValue)[0].issuer_id
          const Communes = await employerDetail.getSingleProfileType(issuerId, 'Communes')
         const category = await employerDetail.getSingleProfileType(issuerId, 'company_type')     
         const location = await employerDetail.getSingleProfileType(issuerId, 'location')    
       Object.assign(values[issuerId], { Communes }, { category }, { location })
       return Object.values(values)

I am getting this kind of result:

[
    {
        "issuer_id": 64,
        "company_name": "Gastro Südtirol",
        "Total_Job": 2,
        "Communes": [],
        "category": [],
        "location": [
            {
                "id": 907,
                "location": "brixen"
            }
        ]
    },
    {
        "issuer_id": 70,
        "company_youtube": "https://www.youtube.com/channel/UCB2bOahchY6Hsc_WXnQ-NCw",
        "company_name": "Auto Hofer",
        "Total_Job": 2
    },
    {
        "issuer_id": 72,
        "company_name": "Assimeran GmbH",
        "Total_Job": 2
    }
 ]

I need “Communes” and “Category” and “Location” data add all the array of json. only it is come first index.

Advertisement

Answer

To get all Ids from your array of objects you should only loop and get issuer_id property on each object.

    const ids = []
    yourObject.forEach(({issuer_id}) => ids.push(issuer_id) )
  //opt2   const ids_2 = yourObject.map(({issuer_id}) => issuer_id)

I am not sure of what you want by “merging the all data to corresponding id”

maybe something like this ?

const hashList = {}
yourObject.forEach( ({issuer_id, ...restParams}) =>{ hashList[issuer_id] = restParams})

now you can access the id you want as easy as hashList[idYouWant] Sorry for my identing, hope this is helpful in any way

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement