Hi coders,
I have doubts about array of objects: I don’t understand how to define and how they can be used.
Are there examples or resource where I can to study ?
Thanks!!!
If you know how to define an object literal, it’s quite simple because you just have to put multiple objects inside an array like so:
const arr = [
{
name: "John Doe"
},
{
name: "foo bar"
}
]
This is a nice data structure to keep your data organized. If you want to perform specific operations to each object/item, you can now loop through the array and find wanted objects.
@camcode it’s a common data structure. Most servers return json which can be literally parsed to Array of Objects.
let myObject = {
name: 'kerafyrm',
age: 38
};
let myObject2 = {
name: 'john snow',
age: 21
};
let myArray = [];
myArray.push(myObject);
myArray.push(myObject2);
console.log(myArray);
[{name: 'kerafyrm', age: 38}, {name: 'john snow', age: 21}];
[object, object] ^^ aka array of objects