如何将此变量的'title'和'firstname'值打印成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'},
];
编辑:
如果我想打印'firstname'值,当我已经知道'title'值时,我怎么能实现呢?
例如,我只想在标题=='EVP Solutions'时打印出firstname。
答案 0 :(得分:1)
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;
编辑:我添加了您在编辑中添加的功能
答案 1 :(得分:0)
即使我因某种原因收到-1来提问,
我想我明白了,
var Chkr;
Chkr = relationshipList;
if(relChkr[1].title == 'EVP Solutions'){
console.log(relChkr[1].name);
}else{
console.log('');
};
不完美,但我现在可以将它扔进div中,所以一切都很好。