1

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",
    "web":"Web",
    "tasktracker":"Task Trackers",
    "jobtracker":"Job Trackers",
    "oozie":"Oozie",
    "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

2
  • 2
    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? Commented Oct 25, 2011 at 21:05
  • 1
    Please clarify your question. It is not clear what you're asking. Commented Oct 25, 2011 at 21:15

1 Answer 1

1
var _key = 'namenode';

for( var i in prettyNames ) {
    // i will container the keys
    if ( i == _key ){
        //prettyName[i] will be your value
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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
yeah im dumb... was thinking loops becaise thats what the question asked for

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.