-4

在下面的代码中,我想打印小于 34 的“years_of_experience”和等于“texas”的出生地的值。但我不知道如何正确编写语句

var  employees = [
    {
       name1:'jacob',
       age2 : 23,
       date_of_join : 23/03/2013,
       years_of_experience : 12,
       birth_place: "virginia" 
    },
    {
       name2:'sheldon',
       age2 : 34,
       date_of_join :2/03/2013,
       years_of_experience : 15,
       birth_place: "lisbon"  
    },
    {
       name3:'johnny',
       age3 : 25,
       date_of_join :29/03/2013,
       years_of_experience : 13,
       birth_place: "texas"  
    }];

    employees.forEach(function(a) {
        console.log(a);
     });
        for(var i = 0;i <= employees.length;i++)
            {
                if(employees[i]!= -1)
                {
                    console.log("gotcha");
                    if(employees.hasOwnProperty === years_of_experience) < 34 || (employees.hasOwnProperty(birth_place) === "teaxs")
                    {
//here i want to print the values of  
//  "years_of_experience"  which is less than 34 and birthplace which is equal to   
// "texas".But i'm not getting how to write the statement for that become m new to 
// javascript and even m not sure i've written the conditional statement right,
// please correct it and help me.. 


                    }
                }
            }
4

3 回答 3

0

像这样

var years = employees[i]["years_of_experience"]; 
var POB = employees[i]["birth_place"];
if (years < 34 || POB === "texas") {
  document.getElementById("idOfSomeTag").innerHTML = "Years:" years +", POB"+POB;
  // or console.log("years",years,"Pob",POB);
}
于 2013-04-20T14:27:33.860 回答
0

尝试这个

if(employees[i].years_of_experience < 34 || employees[i].birth_place === "teaxs")
于 2013-04-20T14:19:59.350 回答
0

首先,你拼错了“texas”

hasOwnProperty()是一种方法,而不是属性,它也不会从属性中提取值,它只是返回一个boolean它是否具有该属性。

您可能需要以下内容

for (var i = 0; i < employees.length; i++) {

    if (employees[i].hasOwnProperty("years_of_experience") && employees[i].hasOwnProperty("birth_place")) {
        if (employees[i].years_of_experience < 34 || employees[i].birth_place === "texas") {
            console.log("Year of Experience: \n" + employees[i].years_of_experience + "\nBirth Place: " + employees[i].birth_place);
        }
    }
}

jsFiddle:检查你的控制台。

如果这是您想要存储日期的方式,您还应该将“date_of_join”更改为字符串。现在你得到一个非常模糊的十进制值。

var employees = [{
    ...
    date_of_join: "23 / 03 / 2013",
    ...
}, {
    ...
    date_of_join: "2 / 03 / 2013",
    ...
}, {
    ...
    date_of_join: "29 / 03 / 2013",
    ...
}];
于 2013-04-20T14:20:21.690 回答