Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何确保javascript检查所有属性_Javascript_Loops_Properties - Fatal编程技术网

如何确保javascript检查所有属性

如何确保javascript检查所有属性,javascript,loops,properties,Javascript,Loops,Properties,嗨,我正在尝试检查联系人是否存在,是否有特定属性,然后返回 var contacts = [ { "firstName": "Akira", "lastName": "Laine", "number": "0543236543", "likes": ["Pizza", "Coding", "Brownie Points"] }, { "firstName": "Harry", "lastName": "Potter", "number":

嗨,我正在尝试检查联系人是否存在,是否有特定属性,然后返回

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 lookUp(firstName, prop) {
    var i;
    for (i = 0; i < contacts.length; i++) {
        if (contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
        } else if (contacts[i].firstName !== firstName) {
            return "No such contact";
        } else if (contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop) === false) {
            return "No such property";
        }
    }
}

lookUp("Harry", "likes");
var联系人=[
{
“名字”:“Akira”,
“姓氏”:“莱恩”,
“编号”:“0543236543”,
“喜欢”:[“披萨”、“编码”、“布朗尼点数”]
},
{
“名字”:“哈利”,
“姓氏”:“波特”,
“编号”:“0994372684”,
“喜欢”:[“霍格沃茨”、“魔法”、“海格”]
},
{
“名字”:“夏洛克”,
“姓氏”:“福尔摩斯”,
“编号”:“0487345643”,
“喜欢”:[“有趣的案例”,“小提琴”]
},
{
“名字”:“克里斯蒂安”,
“姓氏”:“Vos”,
“编号”:“未知”,
“喜欢”:[“Javascript”、“游戏”、“狐狸”]
},
];
函数查找(名字,道具){
var i;
对于(i=0;i
问题是,它只返回循环中的第一个结果,因此在本例中,它只检查联系人[0]的firstName===firstName,然后返回false,即使联系人[1]firstName===firstName

尝试以下操作:

    // Returns:
    //    contact prop if: contact with prop found
    //    true if: contact found, but not prop
    //    false if: not contact found
    function lookUp(firstName, prop) {
        var i, nameFound = false;
        // Starts a loop that looks every contact until the list ends or until firstName with prop is found
        for (i = 0; i < contacts.length; i++) {
            if (contacts[i].firstName === firstName ) {
                // If found, the function return the property "prop" of the contact named "firstName"
                // Note that if there is 2 persons with the same name and both has prop, it returns the first one on the list
                if(contacts[i].hasOwnProperty(prop)){
                    return contacts[i][prop];
                }
                else{
                    nameFound = true;
                }   
            }

        }
        // If the script reach this place, it's because the person wasn't found or was found but hasn't the prop
        return nameFound;
    }
//返回:
//联系道具如果:找到道具联系
//如果:找到联系人,但未找到道具,则为true
//如果未找到联系人,则为false
函数查找(名字,道具){
变量i,nameFound=false;
//开始一个循环,查找每个联系人,直到列表结束或找到带prop的firstName
对于(i=0;i
试试这个:

    // Returns:
    //    contact prop if: contact with prop found
    //    true if: contact found, but not prop
    //    false if: not contact found
    function lookUp(firstName, prop) {
        var i, nameFound = false;
        // Starts a loop that looks every contact until the list ends or until firstName with prop is found
        for (i = 0; i < contacts.length; i++) {
            if (contacts[i].firstName === firstName ) {
                // If found, the function return the property "prop" of the contact named "firstName"
                // Note that if there is 2 persons with the same name and both has prop, it returns the first one on the list
                if(contacts[i].hasOwnProperty(prop)){
                    return contacts[i][prop];
                }
                else{
                    nameFound = true;
                }   
            }

        }
        // If the script reach this place, it's because the person wasn't found or was found but hasn't the prop
        return nameFound;
    }
//返回:
//联系道具如果:找到道具联系
//如果:找到联系人,但未找到道具,则为true
//如果未找到联系人,则为false
函数查找(名字,道具){
变量i,nameFound=false;
//开始一个循环,查找每个联系人,直到列表结束或找到带prop的firstName
对于(i=0;i
函数查找(名字,道具){
var i;
对于(i=0;i
函数查找(名字,道具){
var i;
对于(i=0;i
要检查对象是否具有属性,您只需检查
if(obj[prop])
。 我喜欢这类东西:

function lookUp (firstName, prop) {
    return contacts.reduce(
        function (found, contact) {
            if (contact[prop] && contact.firstName === firstName)
                found.push(contact);
            return found;
        }, []
    );
}

要检查对象是否有属性,您只需选中if(obj[prop])
。 我喜欢这类东西:

function lookUp (firstName, prop) {
    return contacts.reduce(
        function (found, contact) {
            if (contact[prop] && contact.firstName === firstName)
                found.push(contact);
            return found;
        }, []
    );
}

您已经将那些
return
语句编码到循环的中间。因此,如果数组中的第一个对象不匹配,则函数立即返回。你根本不需要那些
else
子句。循环结束后,只需
返回“No-this-property”。
可能结果不清楚(至少对我来说)。如果找到多个结果,该怎么办?谢谢@Pointy的帮助您将
return
语句编码到循环的中间。因此,如果数组中的第一个对象不匹配,则函数立即返回。你根本不需要那些
else
子句。循环结束后,只需
返回“No-this-property”。
可能结果不清楚(至少对我来说)。如果发现多个结果,会发生什么?谢谢@Pointy that worked抱歉,我不得不编辑它以使返回为false;在循环之外。对不起,我必须编辑它以使返回为假;循环外部。它只是在
联系人
数组上迭代,对于每个联系人,如果满足条件,将其推送到保存找到的每个联系人的数组中。解释起来有点复杂,但你可以在a贴的链接中阅读更多内容。您也可以在本书中阅读更多关于迭代器函数的内容。它只需迭代
联系人
数组,对于每个联系人,如果满足条件,将其推送到保存找到的每个联系人的数组中。解释起来有点复杂,但你可以读m