Solved

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

Posted on 2014-07-25
2
249 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now