I have a linear(or single dimension) object with more than 1000 properties within a single record. I have to traverse more than 10000 records. It is possible that sometime the required property doesn't contain by the object within a single record. I want to know what would be the better strategy to traverse them either by looping all the properties of an object or directly check if the key is available in the object by referencing it. Please, check below example to understand this better.
Let's consider an example scenario:
var a = {
"x": 1,
"y": 2,
"z": 3,
"t": 4
}; //linear object
var flagA = false;
var b = {
"x": 10,
"y": 11,
"z": 12
}; //linear object
var flagB = false;
//now I have to check the value of "t" in both objects.
//----------------------------------By use of looping-------------------------------//
for (var i in a) {
if (i == "t") {
flagA = true;
break;
}
}
for (var i in b) {
if (i == "t") {
flagB = true;
break;
}
}
//for object a
if (flagA) console.log("value found in object a for t:", a.t);
else console.log("value not found for t in object a");
//for object b
if (flagB) console.log("value found in object b for t:", a.t);
else console.log("value not found for t in object b");
//--------------------------------------------------------------------------------------//
console.log("\nANOTHER METHOD\n");
//-----------------------------------By use of Key-------------------------------------//
//for object a
if (a["t"]) console.log("value found in object a for t:", a["t"]);
else console.log("value not found for t in object a");
//for object b
if (b["t"]) console.log("value found in object b for t:", b["t"]);
else console.log("value not found for t in object b");
//--------------------------------------------------------------------------------------//
Which one method should I use and Why?