[英]How to check the condition the array contains the element in javascript?
碼:
monthArray = ["jan 2009", ..., "dec 2009"];
jsonFile包含:
[
{
"month": "Aug 2012",
"no_Of_Commits": 1
},
{
"month": "Jun 2012",
"no_Of_Commits": 1
},
{
"month": "Apr 2012",
"no_Of_Commits": 6
}
]
function populate(jsonFile) {
tempJson = jsonFile;
for (var i = 0; i < monthArray.length; i++)
{
if (monthArray.contains(tempJson[i]["month"])) // condition Not working suggest anything else
{
console.log("yes");
tempJson[i]["no_Of_Commits"] = tempJson[i]["no_Of_Commits"];
}
else
{
console.log("NO");
jsonFile[i]["no_Of_Commits"] = 0;
}
}
}
用indexOf
替換contains
if ( monthArray.indexOf( tempJson[i]["month"] ) !== -1 ) {...
當您遍歷一個數組並從另一個數組獲取值時,最好確保兩個數組具有相同數量的索引等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.