0

I have an object:

{
 "id": 12432,
 "application": "pashmodin",
 "unit": null,
 "status": "gholam",
 "issueDate": "1999-06-24T00:00:00",
 "description": "hasan"
}

I want it to become an array like:

[
  {"label": "id", "value": 12432},
  {"label": "application", "value": "pashmodin"},
  {"label": "unit", "value": null},
  {"label": "status", "value": "gholam"},
  {"label": "issueDate", "value": "1999-06-24T00:00:00"},
  {"label": "description", "value": "hasan"}
]

where each object has a label and a value assigned to them. How can I achieve it?

0

2 Answers 2

3

Using Object.entries() and map()

const obj = {"id":12432,"application":"pashmodin","unit":null,"status":"gholam","issueDate":"1999-06-24T00:00:00","description":"hasan"}

const res = Object.entries(obj).map(([label, value]) => ({label, value}))

console.log(res)

Sign up to request clarification or add additional context in comments.

Comments

0

let obj= {
 "id": 12432,
 "application": "pashmodin",
 "unit": null,
 "status": "gholam",
 "issueDate": "1999-06-24T00:00:00",
 "description": "hasan"
}

const arr =[];
Object.keys(obj).forEach((key, i)=> arr.push({label: key, value: Object.values(obj)[i]}))

console.log(arr)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.