2016-12-15 102 views
1

我是全新的,所以請原諒任何失禮。我尋找解決方案,但找不到解決我的問題的任何事情,或者至少我能理解的任何事情。檢查陣列輸入 - Javascript

所以在這裏:我想遍歷數組中的每個對象,並檢查firstName(在本例中爲「Akira」)是否與我的「contacts」數組中的任何firstName匹配。在這個階段,我只想返回對象的索引號(如果可能的話)。如果沒有,請讓我知道如何以最基本的,5歲的方式來做到這一點。謝謝!

var contacts = [ 
{ 
    "firstName": "Akira", 
    "lastName": "Laine", 
    "number": "0543236543", 
    "likes": ["Pizza", "Coding", "Brownie Points"] 
}, 
{ 
    "firstName": "Harry", 
    "lastName": "Potter", 
    "number": "0994372684", 
    "likes": ["Hogwarts", "Magic", "Hagrid"] 
}, 
{ 
    "firstName": "Sherlock", 
    "lastName": "Holmes", 
    "number": "0487345643", 
    "likes": ["Intriguing Cases", "Violin"] 
}, 
{ 
    "firstName": "Kristian", 
    "lastName": "Vos", 
    "number": "unknown", 
    "likes": ["Javascript", "Gaming", "Foxes"] 
} 

function lookUpProfile(firstName, prop){ 
    for (var i = 0; i < contacts.length; i++) { 
    if (contacts[i][firstName] == firstName){ 
     return i; 
    } 
    } 
} 

lookUpProfile("Akira", "likes"); 
+0

var answer = lookUpProfile(「Akira」); –

回答

0

你的代碼中有你就沒有結束「]」爲您的聯繫人的錯誤變量,也當訪問對象屬性使用代碼contacts[i]["firstname"]

var contacts = [ 
 
{ 
 
    "firstName": "Akira", 
 
    "lastName": "Laine", 
 
    "number": "0543236543", 
 
    "likes": ["Pizza", "Coding", "Brownie Points"] 
 
}, 
 
{ 
 
    "firstName": "Harry", 
 
    "lastName": "Potter", 
 
    "number": "0994372684", 
 
    "likes": ["Hogwarts", "Magic", "Hagrid"] 
 
}, 
 
{ 
 
    "firstName": "Sherlock", 
 
    "lastName": "Holmes", 
 
    "number": "0487345643", 
 
    "likes": ["Intriguing Cases", "Violin"] 
 
}, 
 
{ 
 
    "firstName": "Kristian", 
 
    "lastName": "Vos", 
 
    "number": "unknown", 
 
    "likes": ["Javascript", "Gaming", "Foxes"] 
 
}]; 
 

 
function lookUpProfile(firstName, prop){ 
 
    var ret = -1; 
 
    for (var i = 0; i < contacts.length; i++) { 
 
    if (contacts[i]["firstName"] == firstName){ 
 
     ret = i; 
 
    } 
 
    } 
 
    console.log(ret); 
 
} 
 

 

 
    lookUpProfile("Akira", "likes");

0

你可以參考this答案。 以下是訪問JSON對象的最佳方式。

function lookUpProfile(firstName, prop){ 
     for (var i = 0; i < contacts.length; i++) { 
     if (contacts[i]["firstName"] == firstName){ 
      return i; 
     } 
     } 
    } 

function lookUpProfile(firstName, prop){ 
    for (var i = 0; i < contacts.length; i++) { 
    if (contacts[i].firstName == firstName){ 
     return i; 
    } 
    } 
} 
0

對象屬性可以通過accessed點符號,或者你有你的報價鍵它是一個字符串。

if (contacts[i].firstName == firstName){ 
    return i; 
} 

檢查fiddle

0

你忘了收起來觸點陣列,並呼籲物業錯在函數內部

var contacts = [ 
 
\t { 
 
\t \t "firstName": "Akira", 
 
\t \t "lastName": "Laine", 
 
\t \t "number": "0543236543", 
 
\t \t "likes": ["Pizza", "Coding", "Brownie Points"] 
 
\t }, 
 
\t { 
 
\t \t "firstName": "Harry", 
 
\t \t "lastName": "Potter", 
 
\t \t "number": "0994372684", 
 
\t \t "likes": ["Hogwarts", "Magic", "Hagrid"] 
 
\t }, 
 
\t { 
 
\t \t "firstName": "Sherlock", 
 
\t \t "lastName": "Holmes", 
 
\t \t "number": "0487345643", 
 
\t \t "likes": ["Intriguing Cases", "Violin"] 
 
\t }, 
 
\t { 
 
\t \t "firstName": "Kristian", 
 
\t \t "lastName": "Vos", 
 
\t \t "number": "unknown", 
 
\t \t "likes": ["Javascript", "Gaming", "Foxes"] 
 
\t } 
 
]; 
 

 
function lookUpProfile(firstName, prop){ 
 
    for (var i = 0; i < contacts.length; i++) { 
 
    if (contacts[i].firstName === firstName){ 
 
    console.log(i); 
 
     return i; 
 
    } 
 
    } 
 
} 
 

 
lookUpProfile("Sherlock", "likes");

0

您的代碼混淆變量的firstName的關鍵名字。

在你lookUpProfile功能,改變這一行:

if(contacts[i].firstName == firstName) 

Explanation

0

只要改變你的函數一樣 -

function lookUpProfile(firstName, prop){ 
for (var i = 0; i < contacts.length; i++) { 
    if (contacts[i].firstName === firstName){ 
    return i; 
    } 
    } 
} 

並且不要使用 '==' 使用「== ='in javaScript

0

你的代碼有一些小錯誤,但除了那些,一切看起來都很好。第一個錯誤是你沒有關閉聯繫人數組。您已在此處打開陣列:var contacts = [並開始列出聯繫人,但在此之後,您必須使用此字符]關閉此陣列。

第二個問題是你真正關心的問題。當您檢查firstName時,您傳遞的是與您給該函數相同的參數,這就是說,您不在檢查是否contacts[i].firstName === "Akira",而是contacts[i].Akira === "Akira"。而且沒有任何物體具有晃性質。您可以嘗試contacts[i].firstNamecontacts[i].["firstName"]。兩者都可以解決你的問題。

您可以在這裏找到更多的解釋:http://www.w3schools.com/js/js_properties.asp

0

看着你想在函數接受的參數,我假設你想給定的接觸特性。

您可以使用點符號(如contacts[0].firstName)訪問對象的屬性。

你也可以使用數組和關聯鍵來訪問它,如contacts[0]["firstName"]

var contacts = [ 
 
{ 
 
    "firstName": "Akira", 
 
    "lastName": "Laine", 
 
    "number": "0543236543", 
 
    "likes": ["Pizza", "Coding", "Brownie Points"] 
 
}, 
 
{ 
 
    "firstName": "Harry", 
 
    "lastName": "Potter", 
 
    "number": "0994372684", 
 
    "likes": ["Hogwarts", "Magic", "Hagrid"] 
 
}, 
 
{ 
 
    "firstName": "Sherlock", 
 
    "lastName": "Holmes", 
 
    "number": "0487345643", 
 
    "likes": ["Intriguing Cases", "Violin"] 
 
}, 
 
{ 
 
    "firstName": "Kristian", 
 
    "lastName": "Vos", 
 
    "number": "unknown", 
 
    "likes": ["Javascript", "Gaming", "Foxes"] 
 
} 
 
]; 
 

 
function lookUpProfile(firstName, prop){ 
 
    for (var i = 0; i < contacts.length; i++) { 
 
    if (contacts[i].firstName == firstName){ 
 
    console.log(i); 
 
    return contacts[i][prop]; 
 
    } 
 
    } 
 
} 
 

 
console.log(lookUpProfile("Akira", "likes"));

0

好了,所以開始與你的搜索功能:

// Prop parameter wasn't used so I removed it. 
function lookUpProfile(firstName){ 
    for (var i = 0; i < contacts.length; i++) { 
    // If you are using an array accessor for the properties you want to throw 
    // quotes around it. You could also access it like so contacts[i].firstName. 
    // What you had previously was using the value of "firstName" to try to 
    // access the "firstName" property. What your code actually tried was to 
    // access the "Akira" property which doesn't exist. 
    // 
    // Also until you get stronger in javascript it is safer to use the "===" 
    // strict equality as this checks for type and value equality. (ex 0 === "0" will be false) 
    if (contacts[i]['firstName'] === firstName){ 
     // By returning here you are saying that no other object can have the 
     // same "firstName" value. If that isn't true you will want to store 
     // the index in a variable outside of this loop and return it later. 
     // 
     // Another option here is to just return "contacts[i]" that way you 
     // have a reference to the object you are searching for. 
     return i; 
    } 
    } 
} 

一般開始的時候出來我會建議使用嚴格的平等( 「===」/「== 「),因爲它會表現得更直觀,而且會更快失敗。如果你使用「==」/「!=」,javascript會嘗試爲你轉換類型,如果它們不匹配,可能導致意想不到的行爲。

0

如果你寫這個代碼,那麼你會得到像這樣的輸出 [0,undefined,undefined,undefined]。 其中0是您的匹配字符索引。

var lookUpProfile = function(matchString) { var returnVal = function(item,index) { if(item.firstName == matchString) return index; }; return returnVal; };

var profileIndexHolder = contacts.map(lookUpProfile('Sherlock')) console.log(profileIndexHolder);