0

I'm currently trying to replace a for in loop that I have with a regular for loop out of curiosity, but with no success. I always get undefined. Is this even possible with javascript objects?

example object I loop over:

var el = {
    1: {type: "fish", commonName: "clownfish", scientificName: "sdasd", gender: "m", price: 1.99},
    2: {type: "fish", commonName: "dragonfish", scientificName: "dada", gender: "f", price: 2.99}
};

My working for in approach:

for (var element in el) {
   if (el[element].type === type && el.hasOwnProperty(element)) {
       elementNum++;
   }
}

The simple for loop approach that always gets me Cannot read property 'type' of undefined:

for(var i = 0, x = Object.keys(el).length; i < x; i++) {
   if (el[i].type === type && el.hasOwnProperty(i)) {
      elementNum++;
   }
}
1
  • 1
    @Matias has the right answer, but I'm wondering why you aren't using an array? Are some indexes going to be skipped or something? Also, switch the order of (el[i].type === type && el.hasOwnProperty(i)) so that it doesn't check for .type if el[i] doesn't exist (because javascript stops looking through the if AND statement if it finds one that is false. Commented Oct 30, 2016 at 20:23

2 Answers 2

6

You're starting your loop from 0 while your first key in the whole object is 1.

var el = {
  1: {
    type: "fish",
    commonName: "clownfish",
    scientificName: "sdasd",
    gender: "m",
    price: 1.99
  },
  2: {
    type: "fish",
    commonName: "dragonfish",
    scientificName: "dada",
    gender: "f",
    price: 2.99
  }
};

var keyLength = Object.keys(el).length;

// I've simplified the for loop
for (var i = 1; i <= keyLength; i++) {
  console.log(el[i].commonName);
}

Sign up to request clarification or add additional context in comments.

13 Comments

The answer cannot be shorter. Nice!
dammit, how could I not see this.. Thank you!!
@Sebsemillia I would consider that sometimes my mind is like a language hint tool (JSHint, JSLint...) lol
@MatíasFidemraizer hehe ^^
Are there any benefits to this approach versus the original for (var element in el) { method?
|
0

I suggest to iterate directly over the keys of the object.

function getCount(type) {
    return Object.keys(el).reduce(function (r, k) {
        return r + +(el[k].type === type);
    }, 0);
}

var el = { 1: { type: "fish", commonName: "clownfish", scientificName: "sdasd", gender: "m", price: 1.99 }, 2: { type: "fish", commonName: "dragonfish", scientificName: "dada", gender: "f", price: 2.99 } };

console.log(getCount('fish'));

2 Comments

yes, I know this approach as well. I just wanted to try to do it with a simple for loop.. ;) thank you for your answer!
it works only if the object is in a fixed notation for the keys, like indices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.