Home > Mobile >  How to get value from the array of the objects present inside object. (javaScript)
How to get value from the array of the objects present inside object. (javaScript)

Time:10-20

I have an object like below and it contains an Array of objects,

{
  UserId: '634a336122b4b995d25869d5',      
  CategoryId: '620015b6f61454403daba1b4',
  specialUser: [{
        "startDate": "2022-10-3",
        "endDate": "2022-11-3",
        "priceType": 1,
        "price": 199
    },{
        "startDate": "2022-10-3",
        "endDate": "2022-11-3",
        "priceType": 1,
        "price": 199
    },{
        "startDate": "2022-10-3",
        "endDate": "2022-11-3",
        "priceType": 1,
        "price": 199
    }]
}

How to get values from specialUser including UserId and CategoryId

Thanks for help....

CodePudding user response:

data = {
  UserId: '634a336122b4b995d25869d5',      
  CategoryId: '620015b6f61454403daba1b4',
  specialUser: [{
        "startDate": "2022-10-3",
        "endDate": "2022-11-3",
        "priceType": 1,
        "price": 199
    },{
        "startDate": "2022-10-3",
        "endDate": "2022-11-3",
        "priceType": 1,
        "price": 199
    },{
        "startDate": "2022-10-3",
        "endDate": "2022-11-3",
        "priceType": 1,
        "price": 199
    }]
};

Basically you can use map and include the parent properties into your specialUser array

data.specialUser.map((x)=> {
    x.UserId = data.UserId;
    x.CategoryId = data.CategoryId;
    
    return x;
});

  • Related