在下面的代码中我想打印小于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..
}
}
}
答案 0 :(得分:0)
试试这个
if(employees[i].years_of_experience < 34 || employees[i].birth_place === "teaxs")
答案 1 :(得分:0)
首先,你拼错了'德州人'
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",
...
}];
答案 2 :(得分: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);
}