Solved

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

Posted on 2014-07-25
2
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

626 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