Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update array of objects property value on specific condition in angular

I have this array of objects returned from server

{
    "Code": 200,
    "Message": "Success",
    "response": [
        {
            "UserId": null,
            "FullName": test,
            "Status": null,
            "IsActive": 1
        },
        {
            "UserId": null,
            "FullName": null,
            "Status": 'Active',
            "IsActive": 0
        }...
         ...
    ]
}

getting response in List variable

 this.Service.getUser(payload).subscribe(result => {
          this.List = result['response'];
 });

i need some way to manipulate Status value such as if Status is null the assign it Active value.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and again store it in this.List variable without using any loop.

Please suggest some solution.

Thanks

>Solution :

You can make use of map here to loop over response array and add State as Active if Status === null

const apiData = {
  "Code": 200,
  "Message": "Success",
  "response": [{
      "UserId": null,
      "FullName": 'test',
      "Status": null,
      "IsActive": 1
    },
    {
      "UserId": null,
      "FullName": null,
      "Status": 'Active',
      "IsActive": 0
    }
  ]
}

const result = apiData.response.map(o => ({ ...o, Status: o.Status ?? "Active" }));
console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading