Skip to content

Commit 5c85aef

Browse files
committed
Adding object source code and practice questions
1 parent a3800f7 commit 5c85aef

File tree

2 files changed

+187
-1
lines changed

2 files changed

+187
-1
lines changed

Objects/README.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Objects in JavaScript
2+
3+
<p align="center">
4+
<a href="https://youtube.com/live/pYTNGZERlQY">
5+
<img src="https://img.youtube.com/vi/pYTNGZERlQY/0.jpg" alt="Objects In JavaScript" />
6+
</a>
7+
</p>
8+
9+
### Creating an object
10+
```javascript
11+
const person = {
12+
name: "Vishal",
13+
age: 21,
14+
isEducator: true,
15+
skills: ["C++", "JavaScript", "ReactJS"],
16+
projects: {
17+
"Frontend Freaks": "Frontend Development Project",
18+
},
19+
code: function(){
20+
return "start coding";
21+
},
22+
walk: () => {
23+
return "start walking";
24+
}
25+
}
26+
```
27+
28+
### Accessing properties using Dot Operator
29+
```javascript
30+
console.log(person.age); // 21
31+
```
32+
33+
### Accessing properties using []
34+
```javascript
35+
console.log(person["name"]); // Vishal
36+
```
37+
38+
### Checking if a key exists in the object
39+
```javascript
40+
console.log(person.hasOwnProperty("name")) // true
41+
console.log(person.hasOwnProperty("last Name")) // false
42+
```
43+
44+
### Adding, deleting, and updating keys
45+
```javascript
46+
person.name = "Vivek" // Updating name key
47+
person.location = "New Delhi" // Adding location Key
48+
delete person.projects // Deleting projects key
49+
console.log(person);
50+
```
51+
52+
### Shallow Copy
53+
```javascript
54+
const person2 = person
55+
person2.isEducator = false;
56+
```
57+
58+
### Deep Copy
59+
```javascript
60+
const person3 = Object.assign({}, person)
61+
// Nested Objects still do shallow copy here, there for we use lodash cloneDeep method(out of scope for this course)
62+
person3.skills = null;
63+
```
64+
65+
### Using freeze and seal methods
66+
```javascript
67+
Object.freeze(person) // User can't add or delete or update keys
68+
console.log(person);
69+
console.log(Object.isFrozen(person)) // true
70+
```
71+
72+
```javascript
73+
Object.seal(person) // User can't add or delete keys but can update the value
74+
console.log(Object.isSealed(person)); // true
75+
```
76+
77+
### Keys, Values & Entries
78+
```javascript
79+
console.log(Object.keys(person)) // ["name" , "age", "isEducator", ...]
80+
console.log(Object.values(person)) // ["Vishal", 21, true, ...]
81+
console.log(Object.entries(person)) // [["name", "Vishal"], ["age", 21], ["isEducator", true], ...]
82+
```
83+
84+
### Looping through an Object using for...in
85+
```javascript
86+
for (let key in person) {
87+
console.log(key + ":", person[key]); // name: Vishal age: 21, isEducator: true ...
88+
}
89+
```
90+
91+
### Looping through an Object using forEach with Object.keys
92+
```javascript
93+
Object.keys(person).forEach((key) => console.log(key))
94+
```
95+
### How to check if two objects are equal?
96+
```javascript
97+
console.log(Object.is(person, person3))
98+
```
99+
100+
### find count of all players
101+
```javascript
102+
const data = {
103+
id: 1,
104+
name: ["P1", "P4"],
105+
next: {
106+
id: 2,
107+
name: ["P3"],
108+
next: {
109+
id: 3,
110+
name: ["P3", "P4", "P5"],
111+
next: {
112+
id: 4,
113+
name: ["P1", "P2", "P4"],
114+
next: {
115+
id: 5,
116+
name: ["P2", "P3", "P5"],
117+
next: null
118+
}
119+
}
120+
}
121+
}
122+
};
123+
124+
const playerCount = (data) => {
125+
if(data === null){
126+
return {}
127+
}
128+
129+
let countPlayer = {}
130+
for(let player of data.name){
131+
countPlayer[player] = (countPlayer[player] || 0) + 1;
132+
}
133+
const nextPlayerCount = playerCount(data.next);
134+
135+
for(let key in nextPlayerCount){
136+
countPlayer[key] = (countPlayer[key] || 0) + nextPlayerCount[key]
137+
}
138+
return countPlayer;
139+
}
140+
141+
const countPlayer = playerCount(data);
142+
console.log(countPlayer) // {p1: 2, p4: 3, p3: 3, p2: 2: p5: 2}
143+
```
144+
145+
### Prototype and Inheritance in JavaScript Objects
146+
147+
```javascript
148+
const obj1 = {
149+
name: "Vishal"
150+
}
151+
152+
const obj2 = {
153+
age: 21,
154+
__proto__: obj1
155+
}
156+
157+
console.log(obj2.name);
158+
```
159+
160+
### Question 2: Group Anagrams (LeetCode 49)
161+
162+
```javascript
163+
let anagrams = {};
164+
for (let i = 0; i < strs.length; i++) {
165+
const str = strs[i].split("").sort().join("")
166+
if (!anagrams.hasOwnProperty(str)) {
167+
anagrams[str] = []
168+
}
169+
170+
anagrams[str] = [...anagrams[str], strs[i]];
171+
}
172+
return Object.values(anagrams);
173+
```
174+
175+
## Practice Questions
176+
177+
1. [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/)
178+
2. [Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings/)
179+
3. [Two Sum](https://leetcode.com/problems/two-sum/)
180+
4. [Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/)
181+
5. [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)
182+
6. [Integer to Roman](https://leetcode.com/problems/integer-to-roman/)
183+
7. [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
184+
8. [Group Anagrams](https://leetcode.com/problems/group-anagrams/)
185+
9. [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
- [Polyfill of Map, Filter & Reduce](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Array/Polyfill.md)
2929
- [String](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/String/README.md)
3030
- [Recursion](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Recursion/README.md)
31-
- [Linear & Binary Search](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Searching%20Algorthims/README.md)
31+
- [Linear & Binary Search](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Searching%20Algorthims/README.md)
32+
- [Objects](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Objects/README.md)

0 commit comments

Comments
 (0)