I am querying a database and it is returning all of the objects in a collection called contracts. These contracts are simple objects with keys and values. Such as
{
name: BOB,
title: theinternet
date: {...}
}
I would like to check specific keys in each of the objects but I am not sure how to properly describe how these objects are grouped together, and I don't know how to access any of the data inside of this collection of objects.
console.log(contracts)
returns
Array(4) [ {…}, {…}, {…}, {…} ] this spreads to
(4) […]
0: Object { id: "1K19Q7tXIM2yJih7Qj4y", amount: "123421", company: "43214", … }
1: Object { id: "MvEr5t9Sd0oqDyajN9rl", amount: "663425", company: "tewq", … }
2: Object { id: "ckgOLDU6RdstsIoraKiy", amount: "123421", company: "43214", … }
3: Object { id: "jr6XjkAntRucmx0EPeQC", amount: "134", company: "rewq", … }
However, when I console.log(typeof contracts) it says it's an object.
I was able to get the size of the object correctly by doing the following... but I don't know how to search for a specific key in that object.
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
console.log(Object.size(contracts))
returns 4
Once I have all of the objects I would like to be able to if(obj.title === 'theinternet' then do something. But I am unfamiliar with de-structuring this type of object.
Arrayin JS is of typeobject(confusingly), but it has alengthproperty you could use instead of yourObject.sizefunctioncontractsdoes not exist when you try toconsole.logit, not thelengthproperty