Arrays & Objects Flashcards
How do you tell if something is an array?
use Array.isArray(arr) which will return a boolean value
how do you find how many set values an array has?
While thelengthproperty of Array includes unset values in the count,Object.keysonly counts those values that have been set to some value:
What happens if you change an array’slengthproperty to a new, smaller value?
If you change an array’slengthproperty to a new, smaller value, the array gets truncated; JavaScript removes all elements beyond the new final element.
What is a const array?
variables declared withconstand initialized to an array are a little strange; while you can’t change what array the variable refers to, you can modify the array’s contents:
how do you stop an array from being modified?
If you want the elements of the array to also beconst, you can use theObject.freezemethod.
(This is a shallow freeze, objects in the array will still be mutable)
can you use === to compare arrays?
JavaScript treats two arrays as equal only when they are the same array: they must occupy the same spot in memory. This rule holds for JavaScript objects in general; objects must be the same object
What is weird about includes() and indexOf() when used with nested arrays?
includes and indexOfinternally use===to compare elements of the array with the argument. That means we can’t useindexOf or includesto check for the existence of a nested array or an object unless we have a reference to the same object or array we’re looking for:
What is array destructuring?
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
What is the syntax for array destructuring?
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// Expected output: 10
How can we access data from an object?
> person.name // dot notation
= ‘Jane’
> person[‘age’] // bracket notation
= 37
If you have a variable that contains an object keys name, how must you use it?
you must use bracket notation
> let key = ‘name’
> person[key]
How do you delete a property from an object?
> delete person.age
= true
> delete person[‘gender’]
= true
How can we define new methods?
Array.prototype.push = function(newValue) {
this[this.length] = newValue;
}
What is NOT an object or primative?
- variables and other identifiers such as function names
- statements such as
if
,return
,try
,while
, andbreak
- keywords such as
new
,function
,let
,const
, andclass
- comments
- anything else that is neither data nor a function
How do we create a new object that inherits from an existing object?
The static methodObject.createprovides a simple way to create a new object that inherits from an existing object:
let bob = { name: ‘Bob’, age: 22 };
let studentBob = Object.create(bob);
studentBob.year = ‘Senior’;
console.log(studentBob.name); // => ‘Bob’