1
function createDOMNodes( page ) {

    const currentArray = page === 'result' ?
        resultsArray :
        Object.values( favorites );

    // console.log( 'currentArray' , page , currentArray );
    
    currentArray.latest_photos.forEach( (result) => { // do something } );
}

The problem is that there is no latest_photos in the favorites object, I just want to loop the currentArray.forEach when the page parameter is 'favorites'

how can I do that?

It want to switch between different properties when the page is changed. It works fine when I remove the latest_photos property.

Is there a way to Loop through a different array when the page is changed

3
  • Is there a reason you're using .forEach instead of for( of ) ? Commented Oct 22, 2020 at 5:07
  • What is resultsArray, exactly? Commented Oct 22, 2020 at 5:09
  • not at all , I just want to figure out the logic first . Commented Oct 22, 2020 at 5:09

1 Answer 1

1

In the conditional, take the latest_photos instead of the entire resultsArray

const currentArray = page === 'result'
  ? resultsArray.latest_photos
  : Object.values(favorites);

currentArray.forEach((result) => { // do something }
Sign up to request clarification or add additional context in comments.

3 Comments

but this affects my fetch statement, it only returns only 4 photos , for some reason , instead of 10 . is there a reason for that?
There is no fetch in your question..? What code are you referring to? That sounds like a completely separate issue
hey sorry to bother you ! I think there is a issue with the api .

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.