Solved

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

710 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