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

Adding a prefix to the key of an object in axios

I have the object

data = {
    others: [
        {
            code: "A", label: "0-A"
        },
        {
            code: "B", label: "0-B"
        },
        ...,
        {
            code: "N", label: "0-N"
        }
    ]
}

And I need to add the other_ prefix to code value(example, other_N) before sending it to the axios query:

await axios.post(`${URL}`, { data })

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

>Solution :

Something like this

let data = {
  others: [{
      code: "A",
      label: "0-A"
    },
    {
      code: "B",
      label: "0-B"
    },
    {
      code: "N",
      label: "0-N"
    }
  ]
};

let modifiedData = data.others.map(x => {
  x.code = `other_${x.code}`;
  return x;
})

console.log(modifiedData);
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