#7 Javascript Objects | Javascript Hindi Course For Beginners ( 2023 )
A JavaScript object is a collection of named values.
In Simple words Object is used to stored multiple data in single variable.
How to Create Objects
How to access values in object
How to update and create value in object
We can also use function inside object
Constructor function
How to duplicate object
How to loop over Objects
Object Methods
Questions
Question 1
Upvotes vs Downvotes
Given an object containing counts of both upvotes and downvotes, return what vote count should be displayed. This is calculated by subtracting the number of downvotes from upvotes.
@Matt
Question 2
50-30-20 Strategy
The 50-30-20 strategy is a simple way to budget, which involves spending 50% of after-tax income on needs, 30% after tax income on wants, and 20% after-tax income on savings or paying off debt.
Given the after-tax income as ati, what you are supposed to do is to make a function that will return an object that shows how much a person needs to spend on needs, wants, and savings.
@Werdna
Question 3
Extract City Facts
Create a function that takes an object as an argument and returns a string with facts about the city. The city facts will need to be extracted from the object's three properties:
name
population
continent
The string should have the following format: X has a population of Y and is situated in Z (where X is the city name, Y is the population and Z is the continent the city is situated in).
// Example of Objects
const student = {
name:"Rohan kumar",
roll:1,
class:"12"
}
// Object are key-value pairs.
// name -> key , value -> Rohan kumar
// roll -> key , value -> 1
// Inside object we can store any data we want. like boolean , string
// number , array ,objects
// Frist way
const obj = {}
// Second way
const obj = new Object();
const addharCardData = {
firstName:"Anshu",
lastName:"Raj",
dob:"01-01-2000",
adharNo:479382749328749,
address:{ village:"xyz",district:"ranchi",state:"jharkhand",}
}
// two ways to access object values
// first way
const firstName = addharCardData.firstName;
// second way
const first_name = addharCardData["firstName"];
const addharCardData = {
firstName:"Rohan",
lastName:"kumar",
dob:"01-01-2000",
adharNo:479382749328749,
address:{ village:"xyz",district:"ranchi",state:"jharkhand",}
fullName:(){ return this.firstName + " " + this.lastName; }
}
// this -> this refers to current object
// in this example -> this === addharCardData
const fullName = addharCardData.fullName();
// constructor function is another way of creating Object.
// Rule -> Constructor Function Starts with capital letter
function Person (firstName,lastName,age) {
// this === Person
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.fullName = function() {
return this.firstName + " " + this.lastName;
}
}
// constructor function starts with new keyword
const myFather = new Person("John", "Doe", 50);
// Object is reference data type
// what is reference data type ?
/* Answer : Reference data type is a data type that holds a
reference to an object in memory, rather than holding its value directly */
// lets see with example
const obj1 = {title:"obj1"} // allocate space in memory + address
const obj2 = obj1;
// when we assign obj1 to obj2
// no new space created in memory
// only obj2 holds address of obj1
obj2.title = "obj2";
// when we try to change title of obj2
// title of obj1
console.log(obj1.title); // output : obj2
// how to avoide reference
// and create deep clone
// we can use spread operator
const obj1 = {title:"obj1"}
const obj2 = {...obj1};
const student = {
name:"Rohan kumar",
roll:1,
class:"12"
}
// this will give key of object
for( let key in student ){
console.log(key)
}
// for getting key and value same time
for(let key of Object.entries(student)){
console.log(`${key}: ${value}`);
}
// Object.fromEntries
// Covert Map data type to object
const entries = new Map([
['foo', 'bar'],
['baz', 42]
]);
const obj = Object.fromEntries(entries);
console.log(obj);
// Expected output: Object { foo: "bar", baz: 42 }
// Covert Object data type to Map
// Object.entries
Object.entries(student)
console.log(obj);
// Expected output: [ [ 'name', 'Rohan kumar' ], [ 'roll', 1 ], [ 'class', '12' ] ]
cityFacts({
name: "Paris",
population: "2,140,526",
continent: "Europe"
}) ➞ "Paris has a population of 2,140,526 and is situated in Europe"
cityFacts({
name: "Tokyo",
population: "13,929,286",
continent: "Asia"
}) ➞ "Tokyo has a population of 13,929,286 and is situated in Asia"