Search code examples
javascriptjavascript-objects

Removing Object from Array of Array in Javascript


Hey Guys I am exporting some data.
And the challenge I am facing is the data should be in Array of Objects but don't know the data format is Array in Array of Objects.
I can write a script to transform the data to the desired format.
Can anyone help me out with how can I remove the object from the inner array to the outer array?
The data is in this format.

{
  "data": [
    [
      {
        "id": "4101"
      }
    ]
  ]
}

My Resultant Format looks like this Can anyone help me write a javascript to accomplish this objective

{
  "data": [
      {
        "id": "4104",
      }
  ]
}

Solution

  • Please give a try to this :

    var a=obj.data.splice(0, 1);
    obj.data.push(a[0]);
    

    or in one line if you want :

    obj.data.push(obj.data.splice(0, 1)[0]);
    

    Provided you cannot correct the input at the source.