skip to Main Content

I have data that looks like this (sanitized). This is axios output from Azure artefact feeds into response array

{
  "value": [
    {
      "normalizedName": "@test",
      "name": "@test",
      "versions": [
        {
          "normalizedVersion": "1.2.6",
          "version": "1.2.6",
        }
      ],
    },
    {
      "normalizedName": "@test2",
      "name": "@test2",
      "versions": [
        {
          "normalizedVersion": "4.0.1",
          "version": "4.0.1",
        }
      ],
    },
    {
      "normalizedName": "@test3",
      "name": "@test3",
      "versions": [
        {
          "normalizedVersion": "2.2.0",
          "version": "2.2.0",
        }
      ],
    },
  ]
}

and I need to get both the normalizedName and normalizedVersion of every item

eg

[ '@test','1.2.6' ]
[ '@test2','4.0.1' ]
[ '@test3','2.2.0' ]

and so on

I have tried this but cant understand how to get from item 0 to item 1 and so on.
I’m not sure if I need to create a loop counter and put them all into a loop, but seems messy.

for (var key in response.data.value[0]) {

  if (key === 'normalizedName') {
    // console.log(response.data.value[0][key]);
    verList.push(response.data.value[0][key]);
  }

}
for (var key in response.data.value[0].versions[0]) {

  if (key === 'normalizedVersion') {
    // console.log(response.data.value[0].versions[0][key]);

    verList.push(response.data.value[0].versions[0][key]);
  }
}

My output is this:

[ '@test', '1.2.6' ]

I’ve tried other things that get the same result, but just cant get it to go through the full JSON object/body and pull out all the versions.

Appreciate any help.

2

Answers


  1. You can just use reduce and map to get the needed result:

    const res = {
      "value": [
        {
          "normalizedName": "@test",
          "name": "@test",
          "versions": [
            {
              "normalizedVersion": "1.2.6",
              "version": "1.2.6",
            }
          ],
        },
        {
          "normalizedName": "@test2",
          "name": "@test2",
          "versions": [
            {
              "normalizedVersion": "4.0.1",
              "version": "4.0.1",
            }
          ],
        },
        {
          "normalizedName": "@test3",
          "name": "@test3",
          "versions": [
            {
              "normalizedVersion": "2.2.0",
              "version": "2.2.0",
            }
          ],
        },
      ]
    }
    
    console.log('res', res.value.reduce((acc, next) => { acc.push(...next.versions.map(v => [next.normalizedName, v.normalizedVersion])); return acc }, []))
    
    Login or Signup to reply.
  2. const res = {
      "value": [
        {
          "normalizedName": "@test",
          "name": "@test",
          "versions": [
            {
              "normalizedVersion": "1.2.6",
              "version": "1.2.6",
            }
          ],
        },
        {
          "normalizedName": "@test2",
          "name": "@test2",
          "versions": [
            {
              "normalizedVersion": "4.0.1",
              "version": "4.0.1",
            }
          ],
        },
        {
          "normalizedName": "@test3",
          "name": "@test3",
          "versions": [
            {
              "normalizedVersion": "2.2.0",
              "version": "2.2.0",
            }
          ],
        },
      ]
    }
    
    console.log(res.value.map(value => [value.name, value.versions[0].version]))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search