2013-04-20 105 views
-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.. 


        } 
       } 
      } 
+0

[任何具體問題?](http://stackoverflow.com/faq) – Lion 2013-04-20 14:18:30

+0

如果(僱員[I] [ 「years_of_experience」] <34 ||(僱員[i] [「birth_place」] ===「texas」) – mplungjan 2013-04-20 14:19:20

+0

嘿thanx獅子使條件聲明寫,但我將如何打印值,我將從條件後得到真實的 – user1976940 2013-04-20 14:21:44

回答

0

試試這個

if(employees[i].years_of_experience < 34 || employees[i].birth_place === "teaxs") 
+0

嘿thanx如何打印「years_of_experience」和「texas」的價值: – user1976940 2013-04-20 14:24:24

+0

console.log(「經驗年份:\ n」+員工[i] .years_of_experience +「\ nBirth地點:」+ employees [i] .birth_p花邊); – 2013-04-20 14:28:48

+0

逗號更好 – mplungjan 2013-04-20 14:29:36

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", 
    ... 
}]; 
+0

哦,非常感謝ChristopherW,但是如果我想打印「years_of_experience」和「texas」的值,我要說什麼聲明爲此請寫下提及? – user1976940 2013-04-20 14:26:08

+0

@ user1976940現在檢出for循環。 – SomeShinyObject 2013-04-20 14:33:20

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); 
} 
相關問題