2016-05-23 43 views
-1

爲什麼輸出

的翻譯:
能有人幫幫我嗎?
當我打電話與name參數的功能搜索我 需要他給我的所有細節JavaScript對象

<script> 
 
    var friends = {}; 
 
    friends.bill = { 
 
     firstName: "Bill", 
 
     lastName: "Gates", 
 
     number: "(206) 555-5555", 
 
     address: ['Microsoft Way'] 
 
     }; 
 
    friends.steve = { 
 
     firstName: "Steve", 
 
     lastName: "Jobes", 
 
     number: "(444) 111 000", 
 
     address: ["Apple way"] 
 
    }; 
 
    var list = function(obj) { 
 
     for(var key in obj){ 
 
     console.log(obj); 
 
     document.getElementById("demo").innerHTML += key + "<br>"; 
 
     } 
 
    } 
 

 
    var search = function(name) { 
 
     for(var key in friends){ 
 
     if(name === friends[key].firstName){ 
 
      console.log(friends[key]); 
 
      document.getElementById('search').innerHTML += friends[key] + "<br>"; 
 
     } 
 
     } 
 
    } 
 
    // list(friends); 
 
    search("Steve");

回答

0

您需要使用:

console.log(friends[key].firstName + ',' + friends[key].lastName); 

或者:

console.log(JSON.stringify(friends[key])); 

當您登錄的console.log(朋友[關鍵]),它會顯示對象的類型不反對細節

0

因爲friends[key]是一個對象,所以當你嘗試使用它作爲字符串所得到[Object object]

friends[key]; //{"firstName":"Steve","lastName":"Jobes","number":"(444) 111 000","address":["Apple way"]} 

你必須使用對象的屬性之一:

document.getElementById('search').innerHTML += friends[key].firstName + "<br>";

var friends = {}; 
 
    friends.bill = { 
 
     firstName: "Bill", 
 
     lastName: "Gates", 
 
     number: "(206) 555-5555", 
 
     address: ['Microsoft Way'] 
 
     }; 
 
    friends.steve = { 
 
     firstName: "Steve", 
 
     lastName: "Jobes", 
 
     number: "(444) 111 000", 
 
     address: ["Apple way"] 
 
    }; 
 
    var list = function(obj) { 
 
     for(var key in obj){ 
 
     console.log(obj); 
 
     document.getElementById("demo").innerHTML += key + "<br>"; 
 
     } 
 
    } 
 

 
    var search = function(name) { 
 
     for(var key in friends){ 
 
     if(name === friends[key].firstName){ 
 
     alert(JSON.stringify(friends[key])); 
 
      document.getElementById('search').innerHTML += friends[key].firstName + "<br>"; 
 
     } 
 
     } 
 
    } 
 
    // list(friends); 
 
    search("Steve");
<p> 
 
    <span id="demo"></span> 
 
</p> 
 
<p> 
 
    <span id="search"></span> 
 
</p>

0

因爲friends是一個對象,試試這個

var friends = {}; 
 
    friends.bill = { 
 
     firstName: "Bill", 
 
     lastName: "Gates", 
 
     number: "(206) 555-5555", 
 
     address: ['Microsoft Way'] 
 
    }; 
 
    friends.steve = { 
 
     firstName: "Steve", 
 
     lastName: "Jobes", 
 
     number: "(444) 111 000", 
 
     address: ["Apple way"] 
 
    }; 
 
    var list = function(obj) { 
 
     for (var key in obj) { 
 
     document.getElementById("demo").innerHTML += key + "<br>"; 
 
     } 
 
    } 
 

 
    var search = function(name) { 
 
     for (var key in friends) { 
 
      if (name === friends[key].firstName) { 
 
      console.log(friends[key]); 
 
      document.getElementById('search').innerHTML += friends[key].address + "<br>"; 
 
      document.getElementById('search').innerHTML += friends[key].firstName + "<br>"; 
 
      document.getElementById('search').innerHTML += friends[key].lastName + "<br>"; 
 
    document.getElementById('search').innerHTML += friends[key].number + "<br>"; 
 
      } 
 
     } 
 
     } 
 
     // list(friends); 
 
    search("Steve");
<div id="search"></div>

+0

哇約翰 你是親 謝謝:) –