#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.
// 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
 

How to Create Objects

// Frist way const obj = {} // Second way const obj = new Object();

How to access values in 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"];

How to update and create value in object

const addharCardData = { firstName:"Rohan", lastName:"kumar", dob:"01-01-2000", adharNo:479382749328749, address:{ village:"xyz",district:"ranchi",state:"jharkhand",} } // Lets create a new key-value pair addharCardData.fatherName = "Shayam Lal"; // lets modify existing values addharCardData.adharNo = 7394732984739284;

We can also use function inside object

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

// 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);

How to duplicate object

// 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};

How to loop over Objects

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 Methods
// 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' ] ]
// Object.keys // convert object key into array const object1 = { a: 'somestring', b: 42, c: false }; console.log(Object.keys(object1)); // Expected output: Array ["a", "b", "c"]
// Object.values // convert object value into array const object1 = { a: 'somestring', b: 42, c: false }; console.log(Object.values(object1)); // Expected output: Array ["somestring", 42, false]
// hasOwnProperty() // check key exist or not object1.property1 = 42; console.log(object1.hasOwnProperty('property1')); // Expected output: true console.log(object1.hasOwnProperty('toString')); // Expected output: false
 
 

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
// Sample Data getVoteCount({ upvotes: 13, downvotes: 0 }) ➞ 13 getVoteCount({ upvotes: 2, downvotes: 33 }) ➞ -31 getVoteCount({ upvotes: 132, downvotes: 132 }) ➞ 0
 
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
 
// Sample Data fiftyThirtyTwenty(10000) ➞ { "Needs": 5000, "Wants": 3000, "Savings": 2000 } fiftyThirtyTwenty(50000) ➞ { "Needs": 25000, "Wants": 15000, "Savings": 10000 } fiftyThirtyTwenty(13450) ➞ { "Needs": 6725, "Wants": 4035, "Savings": 2690 }
 
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:
  1. name
  1. population
  1. 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).
@jjberg
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"