0

I am getting Json object from backend like below format.

{
"Day-Workers": {
    "0": {
        "Peter": {
            "id": "EJ6609",
            "Sal": "$1000"
        }
    },
    "1": {
        "Sat": {
            "id": "Ej6610",
            "Sal": "$1200"
        }
    }
}}

But I want to convert this object to array of objects format like below.

{
"Day-Workers": [{
        "Peter": {
            "id": "EJ6609",
            "Sal": "$1000"
        }
    },

    {
        "sat": {
            "id": "Ej6610",
            "Sal": "$1200"
        }
    }
]}

Is there way to implement to convert this format, Thanks.

2
  • 2
    Please show us what you have tried. Commented Jun 3, 2021 at 10:16
  • Fix the backend code to return an array instead of an object with numeric keys Commented Jun 3, 2021 at 10:19

1 Answer 1

2

Yes.

obj["Day-Workers"] = Object.values(obj["Day-Workers"]);

var obj = 
{
"Day-Workers": {
    "0": {
        "Peter": {
            "id": "EJ6609",
            "Sal": "$1000"
        }
    },
    "1": {
        "Sat": {
            "id": "Ej6610",
            "Sal": "$1200"
        }
    }
}}

obj["Day-Workers"] = Object.values(obj["Day-Workers"]);
console.log(obj)

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

1 Comment

But the key is not static it will change dynamically and added data also.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.