Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-07-25
2
Medium Priority
?
259 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 2000 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

963 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