Skip to content

Commit 987f685

Browse files
committed
arrays and objects in depth
1 parent 68cbd84 commit 987f685

4 files changed

Lines changed: 200 additions & 3 deletions

File tree

02_basics/01_arrays.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

02_basics/arrays.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//array
2+
//PART 1
3+
4+
const myArr = [0, 1, 2, 3, 4, 5]
5+
6+
const myHeroes= ["shaktiman", "naagraj"]
7+
8+
const myArr2= new Array(1,2,3,4)
9+
//console.log(myArr[1]);
10+
11+
// array methods
12+
13+
//myArr.push(6)
14+
//myArr.pop()
15+
16+
//myArr.unshift(9) //adds 9 in the beginning
17+
//myArr.shift() //removes first element
18+
19+
//console.log(myArr.includes(9));
20+
//console.log(myArr.indexOf(3));
21+
22+
const newArr= myArr.join() //convert to string
23+
24+
//console.log(myArr);
25+
//console.log(typeof newArr);
26+
27+
//slice, splice
28+
29+
//console.log("A ", myArr);
30+
31+
const myn1 = myArr.slice(1, 3) //will show arr including index 1 and 2 only
32+
33+
//console.log(myn1);
34+
//console.log("B ", myArr); //array remains saim as original
35+
36+
37+
const myn2 = myArr.splice(1, 3) //will show arr with element of index 3 as well
38+
//console.log("C ", myArr); //elements spliced will be removed
39+
//console.log(myn2);
40+
41+
42+
//PART 2
43+
44+
const marvel_heros= ["thor", "Ironman", "Spiderman"]
45+
const dc_heroes= ["superman", "flash", "batman"]
46+
47+
// marvel_heros.push(dc_heros)
48+
49+
// console.log(marvel_heros);
50+
// console.log(marvel_heros[3][1]);
51+
52+
// const allHeros = marvel_heros.concat(dc_heros)
53+
// console.log(allHeros);
54+
55+
const all_new_heros = [...marvel_heros, ...dc_heros]
56+
57+
// console.log(all_new_heros);
58+
59+
const another_array = [1, 2, 3, [4, 5, 6], 7, [6, 7, [4, 5]]]
60+
61+
const real_another_array = another_array.flat(Infinity)
62+
console.log(real_another_array);
63+
64+
console.log(Array.isArray("naini"))
65+
console.log(Array.from("naini"))
66+
console.log(Array.from({name: "naini"})) //returns empty array, need to mention keys or values
67+
68+
let score1 = 100
69+
let score2 = 200
70+
let score3 = 300
71+
72+
console.log(Array.of(score1, score2, score3));

02_basics/objects.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//PART 1
2+
3+
//singleton -constructor se banega toh object humesha singleton banega
4+
//Object.create -constructor method
5+
6+
//object literals
7+
const mySym = Symbol("key1")
8+
9+
const JsUser = {
10+
name: "Naini",
11+
"full name": "Naini Maru",
12+
[mySym]: "mykey1", //need to put in square brackets for symbols
13+
age: 20,
14+
location: "Indore",
15+
email: "nainimaru@gmail.com",
16+
isLoggedIn: false,
17+
lastLoginDays: ["Monday", "Saturday"],
18+
}
19+
20+
//console.log(JsUser.email);
21+
//console.log(JsUser["email"]);
22+
//console.log(JsUser["full name"]);
23+
//console.log(JsUser[mySym]);
24+
25+
JsUser.email= "naini@google.com"
26+
27+
//Object.freeze(JsUser) //to lock the info and not be allowed to make changes
28+
29+
JsUser.email= "maru@google.com"
30+
//console.log(JsUser.email);
31+
32+
JsUser.greeting = function(){
33+
console.log("Hello Js User");
34+
}
35+
JsUser.greetingTwo = function(){
36+
console.log(`Hello Js User, ${this.name}`);
37+
}
38+
39+
console.log(JsUser.greeting());
40+
console.log(JsUser.greetingTwo());

02_basics/objects2.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//PART 2
2+
3+
//const tenderUser = new Object() -singleton
4+
const tinderUser= {} //-not singleton
5+
6+
tinderUser.id= "123abc"
7+
tinderUser.name= "Naini"
8+
tinderUser.isLoggedIn= false
9+
10+
//console.log(tinderUser);
11+
12+
const regularUser= {
13+
email: "some@gmail.com",
14+
fullname: {
15+
userfullname: {
16+
firstname: "Naini",
17+
lastname: "Maru",
18+
}
19+
}
20+
}
21+
22+
//console.log(regularUser.fullname.userfullname.lastname);
23+
24+
const obj1 = {1: "a", 2: "b"}
25+
const obj2 = {3: "a", 4: "b"}
26+
const obj4 = {5: "a", 6: "b"}
27+
28+
// const obj3 = { obj1, obj2 }
29+
//const obj3 = Object.assign({}, obj1, obj2, obj4)
30+
//console.log(obj3);
31+
//console.log(obj1);
32+
33+
const obj3 = {...obj1, ...obj2}
34+
// console.log(obj3);
35+
36+
37+
const users = [
38+
{
39+
id: 1,
40+
email: "h@gmail.com"
41+
},
42+
{
43+
id: 1,
44+
email: "h@gmail.com"
45+
},
46+
{
47+
id: 1,
48+
email: "h@gmail.com"
49+
},
50+
]
51+
52+
users[1].email
53+
// console.log(tinderUser);
54+
55+
// console.log(Object.keys(tinderUser)); //-results in an array
56+
// console.log(Object.values(tinderUser));
57+
// console.log(Object.entries(tinderUser));
58+
59+
// console.log(tinderUser.hasOwnProperty('isLoggedIn'));
60+
61+
62+
// ++++++++++++++++++ PART 3 +++++++++++++++++++++++
63+
64+
const course = {
65+
coursename: "js in hindi",
66+
price: "999",
67+
courseInstructor: "hitesh"
68+
}
69+
70+
// course.courseInstructor
71+
72+
const {courseInstructor: instructor} = course
73+
74+
// console.log(courseInstructor);
75+
console.log(instructor);
76+
77+
// {
78+
// "name": "hitesh",
79+
// "coursename": "js in hindi",
80+
// "price": "free"
81+
// }
82+
83+
[
84+
{},
85+
{},
86+
{}
87+
]
88+

0 commit comments

Comments
 (0)