检查数组输入 - Javascript

时间:2016-12-15 06:43:57

标签: javascript arrays

我是全新的,请原谅任何失礼。我搜索了一个解决方案,但找不到任何能回答我问题的内容,或者至少找不到任何我能理解的内容。

所以这里是:我想迭代这个数组中的每个对象,并检查firstName(" Akira",在这种情况下)是否与我的&#中的任何firstNames匹配34;触点"阵列。在这个阶段,我只想返回对象的索引号(如果可能的话)。如果没有,请让我知道如何以最基本的,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");

10 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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; 
   } 
  }   
}

答案 2 :(得分:0)

对象属性可以是accessed点符号,或者你必须引用你的键,因为它是一个字符串。

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

检查fiddle

答案 3 :(得分:0)

您忘记在函数

中关闭联系人数组并调用属性错误

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 i;
   } 
  }   
}

lookUpProfile("Sherlock", "likes");

答案 4 :(得分:0)

您的代码将变量firstName与密钥firstName混淆。

在lookUpProfile函数中,更改以下行:

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

Explanation

答案 5 :(得分:0)

只需更改您的功能 -

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

并且不要使用&#39; ==&#39;使用&#39; ===&#39;在javaScript中

答案 6 :(得分:0)

您的代码有一些小错误,但除了那些,一切看起来都不错。第一个错误是您没有关闭contacts数组。您已在此处打开了数组:var contacts = [并开始列出联系人,但之后,您必须使用此字符]关闭此数组。

第二个问题是你真正关心的问题。当您检查firstName时,您传递的是与该函数相同的参数,也就是说,您不是在检查contacts[i].firstName === "Akira"而是contacts[i].Akira === "Akira"。并且没有任何对象具有Akira属性。

您可以尝试contacts[i].firstNamecontacts[i].["firstName"]。两者都可以解决你的问题。

您可以在此处找到更多解释:http://www.w3schools.com/js/js_properties.asp

答案 7 :(得分:0)

查看您希望在函数中接受的参数,我假设您需要给定联系人的属性。

您可以使用contacts[0].firstName等点符号访问对象的属性。

您还可以使用包含contacts[0]["firstName"]等相关键的数组来访问它。

&#13;
&#13;
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"));
&#13;
&#13;
&#13;

答案 8 :(得分: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将尝试为您转换类型,如果它们不匹配,可能会导致意外行为。

答案 9 :(得分:0)

如果您编写此代码,那么您将得到这样的输出 [0,未定义,未定义,未定义]。 其中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);