christophebazin
christophebazin

Reputation: 200

Nested loops : last loop always returns the last array object

I'm trying to made a loop with 3 arrays. Everything is ok until the last loop.

This last loop always return the last array object.

I tried to add some breaks, move the loops at the same level and many others things. But I've never success...

Here my code

features = [
  'poll',
  'form'
];
actions = [
  'create',
  'activate',
  'duplicate'
];
queries = [
  'createQuery',
  'activateQuery',
  'duplicateQuery'
];
function getFeatureAction(feature) {
  for (f = 0; f < features.length; f++) {
    feature = features[f];

    queries = [
      'createQuery',
      'activateQuery',
      'duplicateQuery'
    ];

    for (a = 0; a < actions.length; a++) {
      action = actions[a]
      featureAction = feature + '_' + action

      for (q = 0; q < queries.length; q++) {
        query = queries[q]
      }

      console.log(featureAction, query)
    }
  }
}

getFeatureAction(); 

My excepted result :

poll_create createQuery
poll_activate activateQuery
poll_duplicate duplicateQuery
form_create createQuery
form_activate activateQuery
form_duplicate duplicateQuery

Upvotes: 0

Views: 471

Answers (3)

Michael Cacciano
Michael Cacciano

Reputation: 389

so I think the general thought here is that you need to log inside of the loop so you can see each iteration of it instead of just seeing the last result which will be printed when the loop finishes since you are outside of it. There are bunch of ways you can do this but staying as close to your version as possible would be something like this.

features = [
  'poll',
  'form'
];
actions = [
  'create',
  'activate',
  'duplicate'
];
queries = [
  'createQuery',
  'activateQuery',
  'duplicateQuery'
];

function getFeatureAction(feature) {
    for (let f = 0; f < features.length; f++) {
    feature = features[f];

    for(let a = 0; a < actions.length; a++) {
      query = queries[a]
      action = actions[a]
      featureAction = feature + '_' + action
      console.log(featureAction, query)
    }

  }
}

getFeatureAction();

note that I only did two for loops because the length of those arrays are the same, but if those are not always going to be the same the same length then you should loop of the last array as well

Upvotes: 1

Jeremy Thille
Jeremy Thille

Reputation: 26380

You're logging the value after the loop is finished, so obviously, you always get the last value. Log it from inside the loop. Also you can write your code in a much more concise way, like this :

const features = [
  'poll',
  'form'
];
const actions = [
  'create',
  'activate',
  'duplicate'
];
const queries = [
  'createQuery',
  'activateQuery',
  'duplicateQuery'
];

function getFeatureAction() {
  for (feature of features) {
    for (action of actions) {
      let featureAction = feature + '_' + action

      for (query of queries) {
        console.log(featureAction, query); // <-- Log INSIDE the loop, not after
      }
    }
  }
}

getFeatureAction();

Upvotes: 0

mbojko
mbojko

Reputation: 14679

Here:

      for (q = 0; q < queries.length; q++) {
        query = queries[q]
      }

      console.log(featureAction, query)

"Loop through the entire queries array, when you're done, print out the last one". Put the console.log inside the loop.

Upvotes: 0

Related Questions