I'm trying to find a match between the breeds array and the dogs array. How can I return the first match that is found. This is what i have that doesn't seem to work and I cant quite find the error. Thanks
我正在尝试找到品种阵列和狗阵列之间的匹配。如何返回找到的第一个匹配项。这是我所拥有的似乎不起作用,我不能找到错误。谢谢
var dogs = ["pug", "husky", "hound", "poodle"];
function findTheBreed(dogs) {
var breeds = ["terrier", "mix", "lab", "hound"];
for(let i = 0; i < dogs.length; i++) {
for (let b = 0; b < breeds.length; b++) {
if(dogs[i] === breeds[b]) {
return breeds[b]
} else {
return "no match"
}
}
}
}
5 个解决方案
#1
2
You're returning false on the first non-match, whereas you should continue iterating.
你在第一次不匹配时返回false,而你应该继续迭代。
move the return "no match"
to after everything has been iterated through.
一切都经过迭代后,将返回“不匹配”移动到。
的jsfiddle
You can alternatively also use the indexOf
here:
您也可以在这里使用indexOf:
function findTheBreed(dogs) {
var breeds = ["terrier", "mix", "lab", "hound"];
for(let i = 0; i < dogs.length; i++) {
if(breeds.indexOf(dogs[i]) != -1){
return dogs[i];
}
}
return "no match";
}
的jsfiddle
One other (in my opinion the best) route would be to use the Array.filter functionality and retrieve the entire array of matches.
另一个(在我看来最好的)路线是使用Array.filter功能并检索整个匹配数组。
var breeds = ["terrier", "mix", "lab", "hound", "snickerdoodle"];
var filtered = breeds.filter(
function (elem) {
return dogs.indexOf(elem) != -1
}
);
return filtered;
的jsfiddle
#2
2
It would be simpler to turn one array into a dictionary and do a direct lookup.
将一个数组转换为字典并进行直接查找会更简单。
For example, if dog is turned into a dictionary
例如,如果将狗变成字典
var dictionary = {};
for(var i=0;i<dogs.length;i++){
dictionary[dogs[i]] = true;
}
for(var i=0;i<breeds.length;i++){
if(dictionary[breeds[i]] === true) return true; //found match
}
#3
2
Or shorter:
或更短:
var findtheBreed=dogs=>["terrier","mix","lab","hound"].find(breed=>dogs.find(dog=>dog===breed))||"no match";
alert(findtheBreed(["pug","husky","hound","poodle"]));
http://jsbin.com/mubatiyebo/edit?console
http://jsbin.com/mubatiyebo/edit?console
using Array.prototype.find , Arrow functions and the brilliant OR operator...
使用Array.prototype.find,Arrow函数和出色的OR运算符......
#4
1
To fix your implementation, move the return "no match"
out of the loop body to the end of your findTheBreed
function.
要修复您的实现,请将循环体中的返回“不匹配”移动到findTheBreed函数的末尾。
However, you can find the first match faster - in constant time - by leveraging constant time Set.has
lookup:
但是,通过利用常量时间Set.has查找,您可以更快地找到第一个匹配 - 在恒定时间内:
function match(dogs, breeds) {
return dogs.find(Set.prototype.has, new Set(breeds));
}
let dogs = ["pug", "husky", "hound", "poodle"];
let breeds = ["terrier", "mix", "lab", "hound"];
console.log(match(dogs, breeds));
Also, I think you should consider renaming your arrays as there is currently no distinction between a dog
and a breed
. Also, you might consider declaring the breeds
as a Set
from the beginning on to avoid redundant conversion.
另外,我认为您应该考虑重命名数组,因为目前狗和品种之间没有区别。此外,您可以考虑从一开始就将品种声明为Set,以避免冗余转换。
#5
0
You're returning out of the function too early. If you want the same data structure, maybe try this:
你太早退出了这个功能。如果你想要相同的数据结构,也许试试这个:
var dogs = ["pug", "husky", "hound", "poodle"];
function findBreed(dogs) {
var breeds = ["terrier", "mix", "lab", "hound"];
for (let i = 0; i < dogs.length; i++){
var dog = dogs[i];
var successIdx = breeds.indexOf(dog)
if (successIdx !== -1){
return dog
}
}
return "no match"
}
#1
2
You're returning false on the first non-match, whereas you should continue iterating.
你在第一次不匹配时返回false,而你应该继续迭代。
move the return "no match"
to after everything has been iterated through.
一切都经过迭代后,将返回“不匹配”移动到。
的jsfiddle
You can alternatively also use the indexOf
here:
您也可以在这里使用indexOf:
function findTheBreed(dogs) {
var breeds = ["terrier", "mix", "lab", "hound"];
for(let i = 0; i < dogs.length; i++) {
if(breeds.indexOf(dogs[i]) != -1){
return dogs[i];
}
}
return "no match";
}
的jsfiddle
One other (in my opinion the best) route would be to use the Array.filter functionality and retrieve the entire array of matches.
另一个(在我看来最好的)路线是使用Array.filter功能并检索整个匹配数组。
var breeds = ["terrier", "mix", "lab", "hound", "snickerdoodle"];
var filtered = breeds.filter(
function (elem) {
return dogs.indexOf(elem) != -1
}
);
return filtered;
的jsfiddle
#2
2
It would be simpler to turn one array into a dictionary and do a direct lookup.
将一个数组转换为字典并进行直接查找会更简单。
For example, if dog is turned into a dictionary
例如,如果将狗变成字典
var dictionary = {};
for(var i=0;i<dogs.length;i++){
dictionary[dogs[i]] = true;
}
for(var i=0;i<breeds.length;i++){
if(dictionary[breeds[i]] === true) return true; //found match
}
#3
2
Or shorter:
或更短:
var findtheBreed=dogs=>["terrier","mix","lab","hound"].find(breed=>dogs.find(dog=>dog===breed))||"no match";
alert(findtheBreed(["pug","husky","hound","poodle"]));
http://jsbin.com/mubatiyebo/edit?console
http://jsbin.com/mubatiyebo/edit?console
using Array.prototype.find , Arrow functions and the brilliant OR operator...
使用Array.prototype.find,Arrow函数和出色的OR运算符......
#4
1
To fix your implementation, move the return "no match"
out of the loop body to the end of your findTheBreed
function.
要修复您的实现,请将循环体中的返回“不匹配”移动到findTheBreed函数的末尾。
However, you can find the first match faster - in constant time - by leveraging constant time Set.has
lookup:
但是,通过利用常量时间Set.has查找,您可以更快地找到第一个匹配 - 在恒定时间内:
function match(dogs, breeds) {
return dogs.find(Set.prototype.has, new Set(breeds));
}
let dogs = ["pug", "husky", "hound", "poodle"];
let breeds = ["terrier", "mix", "lab", "hound"];
console.log(match(dogs, breeds));
Also, I think you should consider renaming your arrays as there is currently no distinction between a dog
and a breed
. Also, you might consider declaring the breeds
as a Set
from the beginning on to avoid redundant conversion.
另外,我认为您应该考虑重命名数组,因为目前狗和品种之间没有区别。此外,您可以考虑从一开始就将品种声明为Set,以避免冗余转换。
#5
0
You're returning out of the function too early. If you want the same data structure, maybe try this:
你太早退出了这个功能。如果你想要相同的数据结构,也许试试这个:
var dogs = ["pug", "husky", "hound", "poodle"];
function findBreed(dogs) {
var breeds = ["terrier", "mix", "lab", "hound"];
for (let i = 0; i < dogs.length; i++){
var dog = dogs[i];
var successIdx = breeds.indexOf(dog)
if (successIdx !== -1){
return dog
}
}
return "no match"
}