0

I have data that looks like this

[
  [ '@test','1.2.6' ],
  [ '@test2','4.0.1' ],
  [ '@test3','2.2.0-unstable' ],
...
]

and I would like to retrieve all the values that have appended -unstable and list both the name @test3 and the value 2.2.0-unstable and perform actions on that

I've tried this but I'm nowhere near

  axios.request(config)
  .then((response) => {
    const myArray = response.data.value.reduce((acc, next) => { acc.push(...next.versions.map(v => [next.normalizedName, v.normalizedVersion])); return acc }, [])

  myArray.forEach(function(value){
        if ((value === '-unstable')) {
          // do some stuff
    } 
  });
2
  • Does this answer your question? How can I access and process nested objects, arrays, or JSON? Commented Dec 13, 2023 at 10:08
  • I checked that but it was just not quite right. Have accepted @mandy8055's answer Commented Dec 13, 2023 at 10:43

1 Answer 1

1

You can try using filter function to filter out only the elements with -unstable in the version then performing your desired actions. Something like:

// ... Your axios code to fetch the data and put to myArray

    // Filter the array to get the values with appended '-unstable'
   const unstableArray = myArray.filter(([_, version]) => version.includes('-unstable'));

   // Perform actions on the filtered values
   unstableArray.forEach(([name, version]) => {
     // Do some stuff...   
    });
});

const myArray = [
  ['@test', '1.2.6'],
  ['@test2', '4.0.1'],
  ['@test3', '2.2.0-unstable'],
  ['@test4', '2.2.10-unstable'],
  ['@test5', '2.2.1-stable'],

];

const unstableArray = myArray.filter(([_, version]) => version.includes('-unstable'));

unstableArray.forEach(([name, version]) => {
  console.log(`Name: ${name}, Version: ${version}`);
});

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.