Javascript Objects

An object is a collection of  properties , defined as a key-value pair. Each property has a key and a value. The property key can be a string and the property value can be any valid value.

Create an object

You can use {} or the new Object().

let object = {}
object.name = "Folau";
object.age = 33;
// or

let obj = new Object();
obj.name = "Folau";
obj.age = 33;

let user = {
    "name": "Folau",
    "age": 33
}
console.log("user: ", user);//{"name":"Folau","age":33}

 

 

Access field value with dot(.)

let user = {
    "name": "Folau",
    "age": 33
}
console.log("user name: ", user.name);//Folau

Access field value with []

let user = {
    "name": "Folau",
    "age": 33
}
console.log("user name: ", user['name']);//Folau

 

Remove or delete a key value with delete.

let user = {
    "name": "Folau",
    "age": 33
}
console.log("user: ", user);//{"name":"Folau","age":33}

/*
* To remove a property, we can use delete operator.
*/
delete user.age;

console.log("user: ", user);{"name":"Folau"}
Check for attribute existence with in
let user = {
    "name": "Folau",
    "age": 33
}
console.log("user: ", user);//{"name":"Folau","age":33}

let doesUserHaveName = ("name" in user);
console.log("name in user: ", doesUserHaveName);//true

let doesUserHaveAddress = ("address" in user);
console.log("address in user: ", doesUserHaveAddress);//false
Loop through attributes of an object with in
let user = {
    "name": "Folau",
    "age": 33
}
for(let field in user){
    console.log("field: ", field, ", value: ", user[field]);
}

//Output
field:  name , value:  Folau
field:  age , value:  33

Objects are mutable

Objects are mutable: They are addressed by reference, not by value. If user is an object, the following statement will not create a copy of user
let user = {
    "name": "Folau",
    "age": 33
}    
let member = user;

member.name = "Lisa";
console.log("user: ", user);//{"name":"Lisa","age":33}
console.log("member: ", member);//{"name":"Lisa","age":33}

The object member is not a copy of user. It is user. Both member and user are the same object.

Any changes to member will also change user, because member and user are the same object.

Clone objects

Use object.assign(target, source…). By cloning an object, changes to that object do not reflect on the original object. 

let person = {
    name:"Folau",
    age: 33,
    score: {
        sc: 100
    }
}
console.log("person ", person);//{name: "Folau", age: 33, score: {sc: 100}}

person.score.sc = 25;
/*
* Clone objects
*/
let lau = Object.assign({}, person);
console.log("lau", lau);//{name: "Folau", age: 33, score: {sc: 25}}

Object.assign does not copy nested objects.

Clone object with JSON.stringify and JSON.parse
copyObject(obj){
    let jsonString = JSON.stringify(obj);
    return JSON.parse(jsonString);
}

 

Source code on Github

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *