0

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.

1
  • for..in is not how you iterate an array in js, you need for..of Commented Dec 11, 2023 at 19:50

2 Answers 2

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 }, []))
Sign up to request clarification or add additional context in comments.

7 Comments

reduce is not needed here
This is the answer I was looking for thank you. It's very similar to @Raghavan Vidhyasagar's answer, but crucially iterates over if there are multiple versions in each normalizedName. I don't have enough reputation to mark this as the answer sorry.
@Anatoly, how would I then traverse the array to find the versions that equal eg 2.2.5?
@gog please show your version of code without reduce
@Anatoly: e.g. value.flatMap(x=>x.versions.map(y=>[x.name, y.version]))
|
1

This JavaScript code defines an object res with a property named "value," which is an array of objects. Each object in the array represents a package with properties such as "normalizedName," "name," and "versions." The "versions" property is an array containing an object with version information.

The code then uses console.log to output an array of arrays. Each inner array contains the package name (value.name) and the corresponding version (value.versions[0].version) extracted from each object in the "value" array. This is achieved using the map function to iterate over the array and create a new array with the specified values for each package.

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]))

3 Comments

plus 1 for the correct answer, minus 1 for the lack of any explanations.
Its simple iteration, so though he/she will understand it just by looking it. My bad edited the answer with explanation.
This was helpful thank you. It didn't get me exactly what I was after (perhaps my question could've been more exact) as it only returns the first normalizedVersion, but in my data there could be many.

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.