0

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

3

1 Answer 1

1

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)

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.