2

So basically I want o loop through an error (what i already did) and print the other key

var planet = "sun";
var planets = [{
    type: "sun",
    desc: "sun"
}, {
    type: "moon",
    desc: "moon"
}];

var pln = planets.length; 
for (var i = 0; i < pln; i++) {
    console.log(planets[i]);
}

How can i I find out if the var planet is in the array planets and print out the description from that key if so?

2
  • So, here you want to find the object that has the type of "sun", and print out the other object's type ("moon")? Commented Mar 3, 2015 at 22:16
  • 6
    FYI - neither the sun nor the moon are planets Commented Mar 3, 2015 at 22:16

3 Answers 3

4

Compare planet with current planets[i].type. Once planet is found in array remember to terminate loop using break keyword:

var pln = planets.length; 
for (var i = 0; i < pln; i++) {
    if (planets[i].type === planet) {
        console.log(planets[i].desc);       
        break;     
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You could use filter

var planet = "sun";
var planets = [{
        type: "sun",
        desc: "sun"
        }, {
        type: "moon",
        desc: "moon"}];

var planetObj = planets.filter(function(obj){ 
 return obj.type==planet;
})[0].desc;//sun

If you are unsure if planet exists, you may wish to see if the array returned from filter actually contained any elements, or perhaps iterate the returned set if there were more than one you were searching for.

4 Comments

It can be just return obj.type == planet, if is not needed.
@dfsq - Yes, not sure why I wrote it as if(){}.
That's going to cause an error if the planet in not in the array. You need to check that the filter returned something before trying to access its desc property.
@blex - Yes, but the OP states that they already know the element is there. This is just showing concept, I am not going to write a whole framework as an answer :) Please see edit indicating the need for error checking.
1

Hmm, I think this is what you want:

var pln = planets.length;
for (var i = 0; i < pln; i++) {
      if (planets[i].type == planet) {
          console.log(planets[i].desc);
          break;
      }
  }

8 Comments

What's the point of simply copying the code of another answer? You forgot to copy what pln is defined as so your code doesn't even work.
Ah, that's a good point. I'll add that. Of course, it leads to the problem that planets is still not defined and still wouldn't work. At some point, one would hope that the user would actually read the code and infer what is going on.
How does this answer differ from this answer which was clearly posted earlier and is an exact copy (except the comparison === v.s. ==)? What value does this answer add? "Please provide a reason for the downvote" > I'd be surprised if it didn't get downvoted if I were you...?
I posted mine seconds after the answer mentioned. I added his break; because I thought it was a good idea, but I didn't need to add it. The pln was requested by @Nit. Not sure what I did wrong? Should I have removed my answer once I saw the answer mentioned? Thanks for helping me become a better member :)
Furthermore, I think that you and @Nit overlooked that my loop is the same as the OP, thus the only thing I really contributed was the if statement.
|

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.