Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9037441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:14:48+00:00 2026-06-16T09:14:48+00:00

I have a javascript application, that calls an api, and the api returns json.

  • 0

I have a javascript application, that calls an api, and the api returns json. With the json, I select a specific object, and loop through that.

My code flow is something like this:
Service call -> GetResults
Loop through Results and build Page

The problem though, is sometimes that api returns only one result, so that means it returns an object instead of an array, so I cant loop through results. What would be the best way to go around this?

Should i convert my object, or single result to an arrary? Put/Push it inside an array? or should I do a typeof and check if the element is an array, then do the looping?

Thanks for the help.

//this is what is return when there are more than one results
var results = {
pages:  [
        {"pageNumber":204},
        {"pageNumber":1024},
        {"pageNumber":3012}
    ]
}

//this is what is returned when there is only one result
var results = {
    pages: {"pageNumber": 105}
}

My code loops through results, just using a for loop, but it will create errors, since sometimes results is not an array. So again, do I check if its an array? Push results into a new array? What would be better. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:14:50+00:00Added an answer on June 16, 2026 at 9:14 am

    If you have no control over the server side, you could do a simple check to make sure it’s an array:

    if (!(results.pages instanceof Array)) {
        results.pages = [results.pages];
    }
    
    // Do your loop here.
    

    Otherwise, this should ideally happen on the server; it should be part of the contract that the results can always be accessed in a similar fashion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an index.html file that calls main.php which returns a json object just
I have a web application that uses TONS of javascript, and as usual, there
I have a node.js (v0.6.12) application that starts by evaluating a Javascript file, startup.js.
Well, here is my problem: I have an application that uses a custom Javascript
I have some pages that reference javascript files. The application exists locally in a
I currently have an application that calls creates and displays charts from various objects'
I have a javascript function called GetRequest() that calls the server with $.ajax() and
I have an ASP.NET MVC 4 application that needs to use an existing API
I have a JavaScript application hosted at x.com which uses AJAX (through jQuery) to
I have a javascript object called blocki that I want to serialize and update

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.