0

My json data currently looks like this:

[
    {
        "timestamp": 345345,
        "status": {
            "code": 200,
            "success": true,
            "message": "Success",
            "errors": []
        },
        "size": 1,
        "nameList": [
            {
                "id": 74997
                "status": "pending"
            }
        ]
    },
    {
        "timestamp": 45,
        "status": {
            "code": 200,
            "success": true,
            "message": "OK",
            "errors": []
        },
        "size": 5,
        "content": [
            {
                "id": 1260087,
                "pilot": "Jujuki",
                "nameId": 4343,
                "RefId": "3453454"
            }
        ]
    }
]

How do I split up this object into 2 different object/arrays of data. I want everything with index 0 to go into the first new object, and everything with index 1 to go into the second new object. What would be best es6 syntax method to use.

1

1 Answer 1

2

you can do Destructuring assignment, like the following:

const data = [{
    timestamp: 345345,
    status: {
      code: 200,
      success: true,
      message: "Success",
      errors: []
    },
    size: 1,
    nameList: [{
      id: 74997,
      status: "pending"
    }]
  },
  {
    timestamp: 45,
    status: {
      code: 200,
      success: true,
      message: "OK",
      errors: []
    },
    size: 5,
    content: [{
      id: 1260087,
      pilot: "Jujuki",
      nameId: 4343,
      RefId: "3453454"
    }]
  }
];


const [firstObj, secondObj] = data;

console.log("the first Object: ", firstObj);
console.log("the second Object: ", secondObj);

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.