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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:28:30+00:00 2026-06-03T19:28:30+00:00

I have a sparse array (indexes are not consecutive) like this: var testArray =

  • 0

I have a sparse array (indexes are not consecutive) like this:

var testArray = { 0: "value1", 5: "value2", 10: "value3", 15: "value4" };

I would simply like to iterate through each item, do some stuff, and be able to break under a certain condition.

I am fairly new to Javascript and I didn’t find a proper way to do it. Here is what I tried:

  1. Built-in “for..in”. It seems that this is not the correct way to iterate through an array

  2. forEach from ECMASCRIPT5. This one iterate correctly, but I cannot break from the loop.

  3. _.each() from Underscore.js. Same result as #2.

  4. $.each() from JQuery. With this one I can break by returning false, but it won’t iterate correctly. For the above example, instead of iterating at 0, 5, 10, 15, it will iterate at 0,1,2,3,4,5,6… which is obviously not what I expect.

So my question is: Is there an easy way to iterate a sparse array with the possibility to break during the loop in Javascript or would it be better to use another data structure like an hashtable? If so, any recommandation?

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-03T19:28:32+00:00Added an answer on June 3, 2026 at 7:28 pm

    What’s wrong with the for...in syntax? You have an object so the for...in syntax is completely valid to use:

    var testArray = { 0: "value1", 5: "value2", 10: "value3", 15: "value4" };
    
    for (var key in testArray) {
      var value = testArray[key];
    
      if (...) {
        break;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have lots of vectors like this one below, very sparse, lots of 'NaN'.
I have a large array 15x15x2200. THis is merely a collection of 15x15 sparse
I have a sparse array in Jscript, with non-null elements occuring at both negative
Suppose I have a 2d sparse array. In my real usecase both the number
I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat
I have a 3d array containing the non-cubic bounding box of a sparse geometry.
I have to implement sparse matrix and do some decompositions like Cholesky Decomposition, LU
I have a sparse array, for example: rare = [[0,1], [2,3], [4,5], [7,8]] I
This thread in SO is about multidimensional array in c+ +. I have to
I have a very sparse Perl matrix (array of arrays), where undef values are

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.