Solved

How to loop thru an js object where greater than a count?

Posted on 2014-07-25
2
255 Views
Last Modified: 2014-07-25
Can't figure out how to loop thru an array (or object) with a "if(count > n)".  Here's some sample code:

var myObj = {"first":"hello", "second":"goodbye", "third":"adios", "fourth":"bonjour", "fifth":"later"};
var length = Object.keys(myObj).length;
console.log(length);   // returns 5

var count = 0;
var myarray = [];

for (key in myObj) {
     if (count < 2) {
          myarray.push(key);
          count++;
     }
}
console.log(myarray);   // returns ["first", "second"]

but if I try to test where count ">" a value, it fails??,  for example:

for (key in myObj) {
     if (count > 2) {
           myarray.push(key);
          count++;
     }
}
it returns empty array "[]"

what the heck am I doing wrong??? thanks in advance!
0
Comment
Question by:kevbob650
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40220652
dont' forget to reset your vars : http://jsfiddle.net/2pk8F/1/
var myObj = {"first":"hello", "second":"goodbye", "third":"adios", "fourth":"bonjour", "fifth":"later"};
var length = Object.keys(myObj).length;
console.log(length);   // returns 5

var count = 0;
var myarray = [];

for (key in myObj) {
     if (count < 2) {
          myarray.push(key);
          count++;
     }
}
console.log(myarray);   // returns ["first", "second"]

//but if I try to test where count ">" a value, it fails??,  for example:

count = 0; // don't forget to reset it
myarray = [];// don't forget to reset it
for (key in myObj) {
    if (count > 2) {
        myarray.push(key);
    }
    count++;
}
console.log(myarray);   // returns ["fourth", "fifth"]

Open in new window

0
 

Author Closing Comment

by:kevbob650
ID: 40220659
Thanks for the quick response!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question