Array
What is an array?
It's a data structure that stores any data type, e.g., strings, integers, objects, or other arrays. If I explain this to a 5-year-old, it would be like stacking toy blocks on top of one another. Now, I couldn't just remove it if I wanted to get to the bottom block, First In Last Out, or zero-indexed.
I would have to pull off the previous blocks.
Reference
Characteristics:
- Javascript Arrays can contain many types of data all at once.
- JS Arrays can shrink or grow in size.
- They are not associative arrays, meaning an array's items (or elements) cannot be retrieved using a string. Similar to an Objects key/value pair.
I'm not sure what this means
- JavaScript array-copy operations create shallow copies. (All standard built-in copy operations with any JavaScript objects create shallow copies, rather than deep copies).
Constructor
Array Constructor
Static Methods
A static method
is a function that only belongs to the class and can not be accessed by the instance of that class.
Example:
const classOfArr = Array().from("foo")
// expected: ['f','o','o']
const instanceB = Array()
// expected: undefined
instanceB.from()
// expected: undefined
Array.from()
Here are some example on how generate a mock list.
Method 1:
Array.from(["Test", "Test", "Test"], (element, index) => element+index)
// expected output: Array ["Test0", "Test1", "Test3"]
Method 2:
Array.from({ length: n }, (_, id) => `user${id}`)
// expected output: Array ["user0", "user1", "user2"...,"userN"]
Array.isArray(value)
Array.of(element1, element2, ..., elementn)
Instance properties
Instance properties
are attributes of an object. Say we have a Person's class, which will come with features when an instance is created like name, age, weight, and height.
Example
class Person(){
const name: string;
const age: number;
const weight: number;
const height: number;
}
Array.prototype.length
Instance methods
Instance methods
are functions of an object. Say we have a Person's class, which will come with behaviors when an instance is created like walk, talk, and jump.