1

I have an object below, that I need in the following format. I can't seem to get the named pairs working. Happily to accept lodash answers as well. Thanks

const object = {
  "mobile": "04000000",
  "address": "123 fake st"
}

That I need to turn into this format.

const format = [
  {
    name: "mobile",
    value: "04000000"
  },
  {
    name: "address",
    value: "123 fake st"
  }
]
Phil
  • 157,677
  • 23
  • 242
  • 245
unicorn_surprise
  • 951
  • 2
  • 21
  • 40

1 Answers1

2

You can use Object.entries to get the key/value pairs of the object as an array, and Array.map to construct the object for each item:

const object = {
    "mobile": "04000000",
    "address": "123 fake st"
}


const res = Object.entries(object).map(e => ({name: e[0], value: e[1]}))

console.log(res)
Spectric
  • 30,714
  • 6
  • 20
  • 43