Skip to content

Commit dc9fc23

Browse files
authored
Update 100_JS_Que
1 parent 6bb2db2 commit dc9fc23

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

100Questions/100_JS_Que

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,87 @@ console.log(func);
605605
output: 5
606606
//delete keyword only use with object properties. here a is a variable so it will not work the variable. //delete user.age
607607
------------------------------------------------------------------------------------------
608+
Dynamic property of object :
609+
const property = "firstName";
610+
const name = "Priya";
608611

612+
const user = {
613+
property : name //{"property" : "Priya"}
614+
}
615+
console.log(user);
616+
617+
const user1 = {
618+
[property] : name //dynamic property required [] //{"firstName" : "Priya"}
619+
}
620+
console.log(user1);
621+
------------------------------------------------------------------------------------------
622+
const user ={
623+
name : "priya",
624+
age : 100
625+
}
626+
627+
//iterate through keys
628+
for(let item in user){
629+
console.log(item) //name age
630+
}
631+
//iterate through values
632+
for(let item in user){
633+
console.log(user[item]) //priya 100
634+
}
635+
------------------------------------------------------------------------------------------
636+
const user ={
637+
name : "priya",
638+
age : 100
639+
}
609640

641+
//double the age as 200 //iterate through keys
642+
for(let item in user){
643+
if(typeof user[item] === "number"){
644+
user[item]*=2
645+
}
646+
}
610647

648+
console.log(user)
649+
------------------------------------------------------------------------------------------
650+
const a = {}
651+
const b = {key : "b"}
652+
const c = {key : "c"}
653+
654+
a[b] = 123;
655+
a[c] = 456;
656+
console.log(a[b]); //456
657+
658+
//console.log(a) //{"[object Object]" : 456}
659+
//here object is not converted into a string so printing key as object Object.
660+
//so for both it will be
661+
//a["[object Object]"] = 123;
662+
//a["[object Object]"] = 456;
663+
//it get override by 456.
664+
------------------------------------------------------------------------------------------
665+
const user = {
666+
name :"priya",
667+
age : 100
668+
}
669+
//convert into a string
670+
const str = JSON.stringify(user)
671+
console.log(str)//{'name':'priya','age':100}"
611672

673+
//convert string onto an object
674+
console.log(JSON.parse(str)) //{ name : "priya, "age":100}
612675

613676

677+
*****************
678+
Real Usecases : Storing in local storage. We can't store the object as a value so require to convert into a string.
679+
const user = {
680+
name :"priya",
681+
age : 100
682+
}
683+
console.log(JSON.stringify(user)) //convert into a string
684+
console.log(JSON.parse(JSON.stringify(user))) //convert into an object
685+
686+
localStorage.setItem("testAsKey", JSON.stringify(user))
687+
------------------------------------------------------------------------------------------
688+
614689

615690

616691

0 commit comments

Comments
 (0)