2016-12-15 108 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);