Name
Code Writing
Description
Example
Associative array (OBJECT)
let obj = {
property1: value1,
property2: value2,
property3: value3,....
}
Create a new object
let obj = {
name: 'Katya',
secondName: 'Stelmakhova',
age: 23,
favoriteNumbers: [3,7,27]}
Accessing property
object.property
Access to the object's property
let obj = {
name: 'Katya',
secondName: 'Stelmakhova',
age: 23,
scores: [5,8,3,6]
}
alert(obj.age) (23)
alert(obj.scores[1]) (8)
Changing value in object's property
Adding property with value to object
object.property = value
Changing value in the object's property
Adding property with value to object
let obj = {
name: 'Katya',
secondName: 'Stelmakhova',
age: 23,
scores: [5,8,3,6]
}
obj.name = 'Nastya';
obj.phone = 0729105261
obj = {
name: 'Nastya',
secondName: 'Stelmakhova',
age: 23,
scores: [5,8,3,6],
phone: 0729105261
}
delete
delete object.property
Deleting object's property
let obj = {
name: 'Katya',
secondName: 'Stelmakhova',
age: 23,
scores: [5,8,3,6],
phone: 0729105261
}
delete obj.phone
obj = {
name: 'Nastya',
secondName: 'Stelmakhova',
age: 23,
scores: [5,8,3,6]
}
in
'property' in object
Checking if property exists in object
You need quotation marks('..')
let obj = {
name: 'Katya',
secondName: 'Stelmakhova',
age: 23,
scores: [5,8,3,6],
phone: 0729105261
}
alert(phone in obj)
alert(status in obj)
true
false
for in
for(let propertyName in object){
.............
}
Checking all properties in object
You do NOT need quotation marks('..')
let obj = {
name: 'Katya',
secondName: 'Stelmakhova',
age: 23,
scores: [5,8,3,6],
phone: 0729105261
}
for(propertyName in obj){
console.log(propertyName)
}
name
secondName
age
scores
phone
Creating object's properties' names
let name = 'propertyName' ; let name2 = 11
let obj = {
[name]: 43,
['age_'+name2]: 'Oskar'
}
Creating object's properties' names
Result from the second line: obj = {
propertyName: 43,
age_11: 'Oskar'
}
Creating object from variable
let var = 'value'; let var1 = 3; let var3 = [el,el,el]
let obj = {
var,
var1,
var3
}
Creating object from variables
Result from the second line: obj = {
var: 'value',
var1: 3,
var3: [el,el,el]
}
Object.fromEntries()
let baseArr = [['name','Katya'],['age',23],['scores',[2,4,5]],]
let obj = Object.fromEntries(baseArr)
Creating object from array
Result from the second line: obj = {
name: 'Katya',
age: 23,
scores: [2,4,5]
}
Spread operation
let newObj = {...baseObj}
Copying Object
Need to be careful , can NOT copy array because object's property contains just an array's address
For full normal copying better use JSON method
(look at the Array Table=
obj = {
name: 'Katya',
age: 23,
surname: 'Stelmakhova'
}
let newObj = {...obj}
Object.assign
spread operation
Object.assign(objectWhereToCopy, objectFromToCopy, objectFromToCopy)
obj1 = {...obj1, ...obj2, ...obj3}
Copying properties from one objects to another. Object to copy from will not be deleted. Object where to copy will fill up with new properties
obj1 = {
name: 'Katya'
}
obj2 = {
age: 23
}
obj3 = {
surname: 'Stelmakhova'
}
Object.assign(obj1, obj2, obj3)
or
obj1 = {...obj1, ...obj2, ...obj3}
obj1 = {
name: 'Katya',
age:23,
surname: 'Stelmakhova'}
obj2 = {
age:23 }
obj3 = {
surname:'Stelmakhova' }
Destructurization of object
let {key1, key2, key3} = object
Destructurizate object allowing to get each property of it, so we can change them
obj = {
name: 'Katya',
age: 23,
surname: 'Stelmakhova'
}
let{name} = obj // 'Katya'
Destructurization of object
obj = {
key1: 'value',
key2: 'value',
key3: 'value'
}
function name({key1,key2}){
..............
}
name(object)
Destructurizate object allowing to get each property of it, so we can change them
obj = {
price:2000,
quantity: 10,
surname: 'Stelmakhova'
}
function name (price, quantity){
return price*quantity
}
name(obj)