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 6530683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:47:08+00:00 2026-05-25T09:47:08+00:00

I have an array of objects which I need placed in a certain order,

  • 0

I have an array of objects which I need placed in a certain order, depending on some configuration data. I am having a problem with itterating through the array in the proper order. I thought that if I made the array, and then stepped through with a for loop, I would be able to execute the code correctly. It is working great except in one use case, in which I add the fourth item to the array and then go back to the third.

links[0] = foo
links[1] = bar
links[2] = foobar
links[3] = a_herring
links[4] = a_shrubery

order = [] //loaded from JSON, works has the numbers 1 2 3 or 4 as values
           //in this case:
order[0] = 1
order[1] = 2
order[2] = 4
order[3] = false
order[4] = 3

for(x in order){        
   if(order[x]){
    printOrder[order[x]]=links[x]
    //remember that in this case order[0] would
}

This should give me an array that looks like this:

//var printOrder[undefined,foo,bar,a_shrubbery,foobar]

But when I try to itterate through the array:

    for(x in printOrder){
        printOrder[x].link.appendChild(printOrder[x].image)
        printOrder[x].appendChild(printOrder[x].link)
        printOrder[x].appendChild(printOrder[x].text)
        document.getElementById("myDiv").appendChild(printOrder[x]);
    }

I get foo, bar, foobar, a_shrubbery as the output instead.

I need to either sort this array by key value, or step through it in the correct order.

  • 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-05-25T09:47:09+00:00Added an answer on May 25, 2026 at 9:47 am

    Iterating over the numerically-index properties of Array instances should always be done with a numeric index:

    for (var x = 0; x < printOrder.length; ++x) {
      // whatever with printOrder[x]
    }
    

    Using the “for … in” form of the statement won’t get you predictable ordering, as you’ve seen, and it can have other weird effects too (particularly when you mix in JavaScript frameworks or tool libraries or whatever). It’s used for iterating through the property names of an object, and it doesn’t treat Array instances specially.

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

Sidebar

Related Questions

I am having trouble getting some data from a JSON object which I have
I have an array of objects which contains various kinds of data in it,
I have an array of objects, some of which respond to :description, and I
I have the following array of objects which I need to convert to one
hi i have an array of objects which need to be sorted (alphabet on
I have an array of objects which populate a UITableView . When a user
I have an array of FileReference objects which have several listeners attached to each
I have an array of DataRow objects in my C# project which I would
I have a method which returns an array of fixed type objects (let's say
I have two arrays of System.Data.DataRow objects which I want to compare. The rows

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.