How to add dynamic properties in json response of api in Angular?

Forums AngularHow to add dynamic properties in json response of api in Angular?
Staff asked 2 years ago

Answers (1)

Add Answer
Sameer Palla Marked As Accepted
Staff answered 2 years ago

Try this solution may it helps you to come out.

Example:

const link = "/assets/info-response.json";
newObject=[];
getRequestData(link)
{
return this.http.get<any>(link).toPromise();
}

//assume that this response may
// [
// { "id":"1", "name":"Anna" }
// { "id":"2", "name":"Martin" }
// ]

//now let add new propertie country in response object

getRequestData(link).then( (resObject) =>{
this.newObject.push(
...resObject.Data.map( (obj)=>({ ...obj , country:"India" }) );
);
})

//now your response in newObject is like
// [
// { "id":"1", "name":"Anna","country":"India" }
// { "id":"2", "name":"Martin","country":"India" }
// ]

 

Subscribe

Select Categories