If else数组查找
if else array lookup
我有一个这样的联系人数组:
var contacts = [{
"firstName": "Akira",
"likes": "beer",
}, // other contacts
和一个使用if/else
查找的函数,称为:
lookUpProfile("Akira", "likes");
如果函数同时找到名称为"Akira"
的参数和属性为"likes"
,则返回"beer"
。如果它找不到这样的名字,它应该返回"no such name"
,如果它找不到参数"喜欢",它将返回"no such property"
。
我很高兴看到你对如何更好地写它的建议,但修复我的代码也会很棒。(它返回"undefined"
而不是"no such contact"
)
function lookUpProfile(firstName, prop) {
for (var i = 0; i < contacts.length; i++) {
var name = contacts[i].firstName;
var propz = contacts[i].hasOwnProperty(prop);
if (name == firstName && propz) {
return contacts[i][prop];
} else if (propz !== prop && firstName == name) {
return "no such property";
} else if (firstName !== name && propz == prop) {
return "no such contact";
}
}
}
lookUpProfile("Akira", "lastName");
谢谢!
你正在做的一个错误是hasOwnProperty返回一个布尔值,但你正在将它与字符串中传递的值进行比较。此外,如果您从每个if/else
块返回,则确实不需要else/if
,您可以只使用if
:
你可以这样考虑你的情况:
// store current contact in a variable
var contact = contacts[i];
// get properties from the current contact
var name = contact.firstName;
var propValue = contact[prop];
// check for existence of the passed in first name and property
var hasName = name === firstName;
var hasProp = contact.hasOwnProperty(prop);
// if it has both, return property
if (hasName && hasProp) {
return propValue;
}
// if it only has the name, return 'no such prop'
if (hasName) {
return 'no such prop';
}
// otherwise it has neither so we return 'no such contact'
return 'no such contact';
演示var contacts = [{
"firstName": "Akira",
"likes": "beer",
}];
function lookUpProfile(firstName, prop) {
for (var i = 0; i < contacts.length; i++) {
// store current contact in a variable
var contact = contacts[i];
// get properties from the current contact
var name = contact.firstName;
var propValue = contact[prop];
// check for existence of the passed in first name and property
var hasName = name === firstName;
var hasProp = contact.hasOwnProperty(prop);
// if it has both, return property
if (hasName && hasProp) {
return propValue;
}
// if it only has the name, return 'no such prop'
if (hasName) {
return 'no such prop';
}
// otherwise it has neither so we return 'no such contact'
return 'no such contact';
}
}
console.log(lookUpProfile("Akira", "likes")); // beer
console.log(lookUpProfile("Akira", "something else")); // no such prop
console.log(lookUpProfile("Someone else", "likes")); // no such contact
或者,代替循环,您可以使用Array.prototype.find按名字查找人员,然后根据find()
的结果返回:
// finds the person with the provided name or return undefined
var contact = contacts.find(function(c) {
return c.firstName === firstName;
});
// if no contact exists, return 'no such contact'
if (!contact) {
return 'no such contact';
}
// if contact doesn't have the prop, return 'no such prop'
if (!contact.hasOwnProperty(prop)) {
return 'no such prop';
}
// otherwise return the prop value
return contact[prop];
演示var contacts = [{
"firstName": "Akira",
"likes": "beer",
}];
function lookUpProfile(firstName, prop) {
// finds the person with the provided name or return undefined
var contact = contacts.find(function(c) {
return c.firstName === firstName;
});
// if no contact exists, return 'no such contact'
if (!contact) {
return 'no such contact';
}
// if contact doesn't have the prop, return 'no such prop'
if (!contact.hasOwnProperty(prop)) {
return 'no such prop';
}
// otherwise return the prop value
return contact[prop];
}
console.log(lookUpProfile("Akira", "likes")); // beer
console.log(lookUpProfile("Akira", "something else")); // no such prop
console.log(lookUpProfile("Someone else", "likes")); // no such contact
我想你从nem035的帖子中得到了答案。
如果您想探索另一种方法,您仍然可以看到此片段。
你可以使用filter属性来首先过滤与名字匹配的json对象。然后检索通过prop
传递的键的值。
var contacts = [
{
"firstName": "Akira",
"likes": "beer",
}]
function lookUpProfile(firstName, prop){
var _toReturn =""
//_inArray will only have matched json object
var _inArray=contacts.filter(function(item){
return item.firstName = firstName;
})
if(_inArray.length !==0){
console.log(_inArray[0])
_toReturn =_inArray[0][''+prop+''];
}
// here you can also check if key prop exist in current object
else if(_inArray.length ==0) {
_toReturn="no such contact";
}
return _toReturn;
}
document.write('<pre>'+lookUpProfile("Akira","likes")+'</pre>')
JSFIDDLE
您的代码永远不会执行整个for循环。在循环的第一个元素上,所有选项都返回一个值。
你的函数正在尝试做太多事情。如果将功能分成更小的函数,工作起来会更好,也更容易。
我将首先拆分查找联系人的功能:
function findContact(firstName) {
for (var i = 0; i < contacts.length; i++) {
var name = contacts[i].firstName;
if(name === firstName) {
return contacts[i];
}
}
// contact was not found.
return undefined;
}
function lookUpProfile(firstName, prop) {
var contact = findContact(firstName);
// undefined is a falsy value
if(!contact) {
return "no such contact";
}
// no need to else because the if branch terminates the function with a return
var propz = contact.hasOwnProperty(prop);
if(!propz) {
return "no such property";
}
// again, no need to add an else
return contact[prop];
}
lookUpProfile("Akira", "lastName");
然后,您可以使用Array.find
将findContact函数代码替换为函数实现,如:
function findContact(firstName) {
return contacts.find(function(contact) {
return contact.firstName === firstName;
}
}
或者如果你的代码将在ES6上运行,你可以再简化一点:
function findContact(firstName) {
return contacts.find(contact => contact.firstName === firstName);
}
我更喜欢使用for in
循环数组对象。你也应该在循环之外返回你的结果。你可以通过Object.keys()
获得对象键。
var contacts = [{
"firstName": "Akira",
"likes": "beer",
},
{
"firstName":"Suu",
"likes" : "computer"
}];
function lookUpProfile(firstName, prop) {
for (var i in contacts) {
var name = contacts[i].firstName;
var propz = Object.keys(contacts[i])[1];
if (name == firstName && propz == prop) {
result = contacts[i][prop];
} else if (prop != propz && firstName == name) {
result = "no such property";
} else if (firstName !== name && prop == propz) {
result = "no such contact";
}
}
return result;
}
console.log(lookUpProfile("Suu","likes"));
- Lodash 从值数组中查找数组中的值
- 使用JS查找数组的平均值
- 查找数组javascript中包含的元素类型
- Undercore:查找数组中出现频率最高的对象
- 查找数组's按属性不存在于另一个数组中的对象
- 查找数组中项目包含子字符串的索引
- 按键查找数组中对象的索引
- 按链中的键查找数组元素
- 使用JavaScript查找数组中最近的日期
- 在Javascript中查找数组长度
- 正在查找数组中第一个匹配元素的索引
- Rhino 1.5 R5在查找数组差异时未正确评估JavaScript
- 查找数组值
- 如何在javascript中按属性查找数组中的对象
- 如何使用JavaScript查找数组中的最早日期
- 查找数组中最长的单词/字符串
- 如何查找数组中所有出现的元素的索引(Ramda.js方式)
- 查找数组中的下一个最高数字 - jquery
- 如何查找数组中另一个对象旁边的对象
- 按键查找数组中最接近的 2 个元素