Object Knowledge Flashcards
Deep Copy an object
JSON.stringify( )
then
JSON.parse( )
or
JSON.parse(JSON.stringify( ))
Delete from an object
delete myObject.myKey
delete myObject[‘myKey’]
What’s the difference between a deep copy and a shallow copy
Shallow copy- when you shallow copy an object, all you do is copy the first layer data from the heap. This includes the pointers to other objects
Deep copy- A deep copy also makes a copy of all the nested objects and points to those objects accordingly.
Add to an object
dot notation
myObject.newKey =
square bracket notation
myObject[‘newKey’] =
Change an object key’s value
dot notation
myObject.oldKey =
square bracket notation
myObject[‘oldKey’] =
Change an object key’s name
No built in method
Create an object
let myObj = { }
d
f
Shallow copy an object (1 ways)
Does it work with arrays?
1. spread syntax let newObject = { ...oldObject };
What is returned from adding an entry to an objects?
ex.
{a: 1, b: 2, c: 3}.d = 4
4
(and it adds d: 4 to the object.
Why is adding an entry to an object in this way:
{a: 1, b: 2, c: 3}.d = 4
useless?
Because there is no variable pointing to it. You can not no longer retrieve the object you’ve just changed.
What defines an object? (3 things)
- An unordered collection of data in the form of “key: value” pairs.
- JS objects have properties attached to the object.
- These keys can be variable or functions (called methods).
How do objects, functions and arrays differ?
Object-they can have properties and methods
Array- they can have properties and methods just like any other object
BUT they have an ordered collection
Function- they can have properties and methods just like any other object
BUT they can be called
let myObject = { apple: 'berry', purrple: 'tomato' }
If you want to log the values to console within a for…in statement:
for (let key in myObject) {
??????
}
How do you do it? (bracket or dot notation)
Bracket notation
for (let key in myObject) {
console.log(myObject[key]);
}
What happens if you use myObject.key instead of bracket notation?
(scroll down)
myObject.key
converts key to a string. it will return undefined for each key.
What is an object property?
a key: value pair
What is the object property name?
the key
How to:
- add and
- access
multiword object keys
Use apostrophes.
let anObject = {‘a key’ : ‘a property’}
Use bracket notation
anObject[‘a key’]
Input a variable as a key
Use bracket notation let aVariable = 'apple'; let user = { [aVariable]: 2 }
let user = { name: "John", age: 30 };
let key = prompt(“What do you want to know about the user?”, “name”);
alert( user[key] )
It does not work with dot notation
What is dot notation
What is square bracket notation
dot notation
myObject.newKey =
square bracket notation
myObject[‘newKey’] =
What is a computed property?
A key that created using the value of a variable:
let apple = 123; let myObject = { [apple] : 1234}; console.log(myObject) // { '123' : 1234 }
When to use dot vs square notation
Dot for known and simple names.
Use square when you need it.
Does this throw an error?
function makeUser(name, age) { return { name: name, age: age, }; }
No.
Dot notation keys are converted to strings. So the keys name and age, don’t register as variables.
What is property value shorthand?
When creating an object where the key name and the variable name used as the value are the same, you can:
let name = 'apple'; let anObject = { name, }
Is the same as:
let name = 'apple'; let anObject = { name: name } which gives {name: 'apple'}
Which of these have the correct spacing let anObject = {
name: ‘John’
}
or
let anObject = { name : 'John'
}
or
let anObject = { name : 'John' }
or
let anObject = {
name : ‘John’
}
let anObject = { name: 'John' }
It's acceptable to use language reserved names as object keys: let obj = { for: 1, let: 2, return: 3 }; T or F
True
They are converted to strings, and called as strings. So it doesn’t interfere with regular code function.
Numbers aren’t allow as variables, so why does this work:
let obj = { 0: "test" // same as "0": "test" };
// both alerts access the same property (the number 0 is converted to string “0”)
alert( obj[“0”] ); // test
alert( obj[0] ); // test (same property)
In the object creation, 0 is converted to a string
In this:
alert( obj[0] )
because numbers aren’t allowed as variables, 0 isn’t a potential variable. JS converts it to string (since object keys are strings)
Return Boolean based on whether a key is a part of an object (or a variable containing the key)
Why use it instead of just checking whether anObject.aKey exists?
'key' in anObject (use quotation marks or bracket notation or it won't look for a string by default) For example with a variable: let variable = 'key' variable in anObject
In case the key value is explicitly ‘undefined’
How are objects ordered…
if the keys are strings?
if the keys are numbers (as strings)?
if they are a mix of strings and numbers(as strings)?
if the keys are strings
They are ordered by creation order
if the keys are numbers (as strings)
They are ordered by number
if they are a mix of strings and numbers(as strings)?
Numbers are pushed to the beginning
If object keys are numbers, they will auto rearrange. How can you stop this?
The properties are only reordered IF they are integer properties
So you can cheat by naming the keys
“+10”
“+3”
and they will keep the order (but have a + in them)