Mark Gabb
Mark Gabb

Reputation: 55

Issues with converting an array of objects into a different format

I'm struggling to convert an array that I've been given into a useful format:

Given

{
  "myValues": [{
    "0": {
      "id": "5ed32599-3c4d-49ad-8a1a-79bbc39a3e02",
      "value": "my Value",
      "value2": "my Value 2"
    },
    "1": {
      "id": "5ed32599-3c4d-49ad-8a1a-79bbc39a3e02",
      "value": "my Value",
      "value2": "my Value 2"
    },
    "id": "5ed32599-3c4d-49ad-8a1a-79bbc39a3e02"
  }]
}

I am trying to remove the numbers in front of the objects and just the ID that sits outside of the two inner objects.

Therefore I would then be given:

{
  "myValues": [{
      "id": "5ed32599-3c4d-49ad-8a1a-79bbc39a3e02",
      "value": "my Value",
      "value2": "my Value 2"
    },
    {
      "id": "5ed32599-3c4d-49ad-8a1a-79bbc39a3e02",
      "value": "my Value",
      "value2": "my Value 2"
    }
  ]
}

I have been trying to use different ways of mapping Objects into new formats but I'm really struggling particularly to get rid of the IDs

Upvotes: 0

Views: 36

Answers (1)

charlietfl
charlietfl

Reputation: 171669

You can use Object.values().

I added a filter() to remove the single id...not sure if that is a typo in example data or not...or if you also want it included in results

const myValues=[  
      {  
         "0":{  
            "id":"5ed32599-3c4d-49ad-8a1a-79bbc39a3e02",
            value : 'my Value',
            value2 : 'my Value 2'
         },
         "1":{  
            "id":"5ed32599-3c4d-49ad-8a1a-79bbc39a3e02",
            value : 'my Value',
            value2 : 'my Value 2'
         },
         "id":"5ed32599-3c4d-49ad-8a1a-79bbc39a3e02"
      }
   ]
   
   const arrValues = Object.values(myValues[0]).filter(el => typeof el === 'object')
   
   console.log(arrValues)

Upvotes: 1

Related Questions