Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How would I print the 'title' and 'firstname' values from this variable into a div?

var relationshipList = [
    { name: 'William Cotterll',           firstname: 'William',  title: 'EVP Solutions' },
    { name: 'Edwin Van Der Saar',         firstname: 'Edwin',  title: 'Security Specialist' },
    { name: 'Kelly Woodson',              firstname: 'Kelly',  title: 'Production Manager'},               
];

EDIT:

If I wanted to print out the 'firstname' value, when I already knew the 'title' value, how could I achieve that?

For example, I only want to print out firstname when the title == 'EVP Solutions'.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
186 views
Welcome To Ask or Share your Answers For Others

1 Answer

var toInsert = '';

for (var i = 0, l = relationshipList.length; i < l; i++) {
    toInsert += relationshipList[i].title + ' ';
    if( relationshipList[i].title == 'EVP Solutions'){
        toInsert += ', ' + relationshipList[i].firstname + ' ';
    }
}

document.getElementById('#yourDivId').innerHTML = toInsert;

EDIT: I've added functionality that you added in your edit


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...