简体   繁体   English

正确使用array.map

[英]Using array.map properly

I am not sure why my array.map function returns undefined but my for loop returns the result. 我不确定为什么我的array.map函数返回未定义,但是我的for循环返回结果。 When I console log map it displays: 当我控制台日志映射时,它显示:

Tom Dick Harry 汤姆·迪克·哈里

and when console log for loop it shows 并在控制台日志中显示循环时

['Tom', 'Dick', 'Harry'] ['汤姆','迪克','哈里']

Not sure if this is the problem of showing undefiend. 不知道这是否是显示未防御的问题。

var friends = ["Tom", "Dick", "Harry"];
var secondLevelFriends = ["Anne", "Harry", "Quinton"];
var allUsers = ["Tom", "Dick", "Harry", "Anne", "Quinton", "Katie", "Mary"];

function findPotentialFriends(existingFriends) {
  return function(x) {
    // existingFriends.map(function(elem) {
    //   if(elem === x) return false;
    //   else return true;
    // })
    for(var i = 0; i < existingFriends.length; i++) {
      if(existingFriends[i] === x) return false;
      else return true;
    }
  }
}

var isNotAFriend = findPotentialFriends( friends );
isNotAFriend(allUsers[0]); // false

First of all your for loop has some logic problems. 首先,您的for循环存在一些逻辑问题。 it should be written as so: 它应该这样写:

var foundFriend = false;
for(var i = 0; i < existingFriends.length; i++) {
  if (existingFriends[i] === x)
  {
    foundFriend = true;
    break;
  }
}
return !foundFriend;

The reason why isNotAFriend evaluates to undefined is because you forgot a return statement. isNotAFriend评估为undefined的原因是因为您忘记了return语句。 Furthermore, the Arrays.map just simply takes each element of the array, passes it into the function, then creates a new array based on what your function returns. 此外, Arrays.map只是简单地获取数组的每个元素,将其传递给函数,然后根据函数返回的内容创建一个新的数组。 What you might be searching for is perhaps Array.some see docs . 您可能要搜索的可能是Array.some see docs Example: 例:

 var friends = ["Tom", "Dick", "Harry"]; var secondLevelFriends = ["Anne", "Harry", "Quinton"]; var allUsers = ["Tom", "Dick", "Harry", "Anne", "Quinton", "Katie", "Mary"]; function findPotentialFriends(existingFriends) { return function(x) { return !existingFriends.some(function(elem) { return elem === x; }); } } var isNotAFriend = findPotentialFriends( friends ); console.log(isNotAFriend(allUsers[0])); 

The problem with your code is that the return statements in the callback just return to map() , they don't return to the original caller. 您的代码的问题在于,回调中的return语句仅返回map() ,而不会返回原始调用者。 map() collects all these return values into an array and returns this, but you never do anything with the result of existingfriends.map() . map()将所有这些返回值收集到一个数组中并返回该数组,但是您绝不会对existingfriends.map()的结果做任何事情。

Javascript has a built-in function to search an array for a matching element, Array.prototype.indexOf() . Javascript具有内置功能,可以在数组中搜索匹配的元素Array.prototype.indexOf() It returns -1 if no match is found. 如果找不到匹配项,则返回-1

So you can simply write: 所以你可以简单地写:

 var friends = ["Tom", "Dick", "Harry"]; var secondLevelFriends = ["Anne", "Harry", "Quinton"]; var allUsers = ["Tom", "Dick", "Harry", "Anne", "Quinton", "Katie", "Mary"]; function findPotentialFriends(existingFriends) { return function(x) { return existingFriends.indexOf(x) == -1; } } var isNotAFriend = findPotentialFriends(friends); console.log(isNotAFriend(allUsers[0])); // false 

Simply add the return in map() function .Its one of the new function inside the return function .so apply with return this also. 只需在map()函数中添加return 。它是return函数内部的新函数之一。因此也可以使用return this。

 var friends = ["Tom", "Dick", "Harry"]; var secondLevelFriends = ["Anne", "Harry", "Quinton"]; var allUsers = ["Tom", "Dick", "Harry", "Anne", "Quinton", "Katie", "Mary"]; function findPotentialFriends(existingFriends) { return function(x) { return existingFriends.map(function(elem) { if(elem === x) return false; else return true; }) // for(var i = 0; i < existingFriends.length; i++) { // if(existingFriends[i] === x) return false; // else return true; //} } } var isNotAFriend = findPotentialFriends(friends); console.log(isNotAFriend(allUsers[0])); // false 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM