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
  • 141,914
  • 21
  • 225
  • 223
unicorn_surprise
  • 661
  • 1
  • 10
  • 25

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
  • 27,594
  • 6
  • 14
  • 39