by
1
5
404k
3
Top 1% !
Pearl of Wisdom
Tagged
Specified
OpenSource
Popularity: 2021st place
Languagejavascript
LicenseMIT_X11

Adding an Element in an Array using a Function.

Arrays of Objects

Remember that an object is just another type, like a string or number but more complex. This means that just as we can make arrays of numbers and strings, we can also make arrays of objects.

Here we have our Person constructor which should look familiar. We can use this constructor to make an array of Person objects, similar to how we might make an array of numbers but filling in people instead.

Source: codecademy
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1208860/Adding-an-Element-in-an-Array-using-a-Fu?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// Our person constructor function Person (name, age) { this.name = name; this.age = age; } // Now we can make an array of people var family = new Array(); family[0] = new Person("alice", 40); family[1] = new Person("bob", 42); family[2] = new Person("michelle", 8); // add the last family member, "timmy", who is 6 years old family[3] = new Person("timmy", 6);
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus