Take the 2-minute tour ×

This is only mere example. However the basis of what I am doing is running through a loop where one of the objects in the loop can have one of the below objects what I want to do is find in said object below if there is a match to the object I am looping through where if there is a match I want to use the use the object below matching value.

prettyNames = {
    "namenode": "Name Nodes",
    "secondarynamenode":"Secondary Name Nodes",
    "datanode":"Data Nodes",
    "tasktracker":"Task Trackers",
    "jobtracker":"Job Trackers",
    "single-namenode":"Single NameNode",
    "single-databse":"Single Database"

So with that I know how to do it with PHP and I thought I knew how I could it with javascript. However Its not working as well as I thought so I am looking for ideas how to handle it properly.

Edit What I have is an JSON object I am iterating through via a for each style loop. Where one of the objects within that JSON is a more like an ID from the system output. What I want to do is while running through that loop take that object as a variable and compare it to the variables/objects in the prettyNames object so if its found in prettyNames it will use that value instead of the default system output. Ultimately I am also trying to avoid having eventually dozens of nested if statements to do the same thing

share|improve this question
I've read it a few times but still don't completely understand it. Do you have an array of objects you're looping through? –  pimvdb Oct 25 '11 at 21:05
Please clarify your question. It is not clear what you're asking. –  jfriend00 Oct 25 '11 at 21:15

1 Answer 1

up vote 1 down vote accepted
var _key = 'namenode';

for( var i in prettyNames ) {
    // i will container the keys
    if ( i == _key ){
        //prettyName[i] will be your value
share|improve this answer
There's no point in looping through prettyNames. Your code could replaced with this: if (prettyNames[_key]) or this: if (_key in prettyNames). Though, I will have to admit that I cannot tell what the OP is actually asking. –  jfriend00 Oct 25 '11 at 21:13
I just need to take var and compare it to something in prettyNames where if its found in prettyNames use the prettyNames value of the object found, however if its not found just use what the var is by defualt –  chris Oct 25 '11 at 21:25
yeah im dumb... was thinking loops becaise thats what the question asked for –  locrizak Oct 25 '11 at 23:30

Your Answer


