我的错误在哪里?我知道这与if语句与循环相关的位置有关,但我不能确定。目标是让函数检查对象是否具有某个属性,然后返回该属性。
//Setup
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) {
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
}
else if (firstName !== contacts[i].firstName) {
return "No such contact";
}
return "No such property";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Kristian", "lastName");发布于 2018-04-30 07:26:08
正如我在注释中所说的,您需要移动失败条件,以便for循环能够在返回之前检查必要的索引:
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) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
return "No such property";
}
}
return "No such contact";
}
console.log(lookUpProfile("Kristian", "lastName"));
或者,您可以通过使用Array#find()来采用稍微更函数化的编程方法
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) {
const contact = contacts.find(
contact => contact.firstName === firstName
);
if (contact === undefined) {
return "No such contact";
}
if (contact.hasOwnProperty(prop)) {
return contact[prop];
}
return "No such property";
}
console.log(lookUpProfile("Kristian", "lastName"));
发布于 2018-04-30 07:23:38
您的语句return "No such property";应该在for循环之外。在for循环中,该语句在第一次迭代后终止循环。
此外,您不需要else if (...,因为您将从for循环外部返回它。
//Setup
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) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
}
}
return "No such property";
}
// Change these values to test your function
console.log(lookUpProfile("Kristian", "lastName"))
console.log(lookUpProfile("Sherlock", "lastName"))
OR:如果您对Array#filter很熟悉:
//Setup
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 res = contacts.filter(p => p.firstName === firstName);
if (res.length > 0 && res[0].hasOwnProperty(prop)) {
return res[0][prop];
}
return "No such property";
}
// Change these values to test your function
console.log(lookUpProfile("Kristian", "lastName"))
console.log(lookUpProfile("Sherlock", "lastName"))
发布于 2018-04-30 07:33:49
在Mamun之前,我已经写出了大部分内容,但我想更多地谈谈这个答案和您正在做的事情
对于我的解决方案,我只是简单地将数组过滤为只具有正确名字的内容。如果您确定您的元素将具有不同的名字,那么这是一个很好的解决方案,因为您应该只期望过滤后的数组的长度为0或1。现在,缺点是Array.filter()在整个数组长度中运行,甚至在我们找到person之后也会继续运行。对于一个很短的数组,就像这个例子一样,这不是问题。对于一个有10,000个元素的数组,我们的person是第一个搜索的,有点性能问题。
另一个问题是:如果你有多个名字相同的人,会发生什么?过滤后的数组将大于1,但我们只会使用第一个结果--可能会有问题。
此外,如果您计划频繁地查找信息,则可能需要考虑使用所谓的“散列映射”--它们是听起来很复杂的名称,非常简单。
是什么让一个条目与众不同?在大多数情况下,我们有来自数据库的数据附带的一些不同的ID,但在这种情况下,我们没有。然后我们必须问自己,是什么让"Kristian Vos“不同于"Krtistian Paule”(假设两者都在相同的拉取数据中)。使用我们所知道的关于对象的重要信息(在本例中是名字+姓氏),我们创建一个唯一的键来将数据存储在对象中。这样,我们可以通过简单地使用映射键访问对象来更快地查找事物。
我的观点是:仔细考虑如何处理您的解决方案,并了解小型数据集通常很容易连续遍历,但是假设由于某种原因,您的数组包含10,000或100,000个条目--您是否以最合理的方式处理数据?
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){
let profile = contacts.filter(person=> person.firstName == firstName);
//Get only the person who has the same first name
if(!profile.length) return false;
//If the length is 0, then !0 will be truthy and the function will immediately return false
profile = profile[0];
//this is pure laziness, you could just alter the next line to read profile[0][prop] but I'll be honest i did this for visual appeal
return profile[prop] || false;
// this notation is fucky -- if the property is set on the profile, then it will return that value.
// if it is not set, it'll return undefined, which is a falsey value and will therefore move over to the other side of the || and return that value, which is false.
}
console.log(lookUpProfile('Kristian', 'lastName'));
https://stackoverflow.com/questions/50092184
复制相似问题