filmov
tv
Extract object values in JavaScript. (JavaScript Short Trick)

Показать описание
In this video, I have explained how to extract JavaScript object without keeping same variable name as object property name.
code in video explained
Object named obj, containing properties a,b,c with value 10,20,30
const obj = {
a: 10,
b: 20,
c: 30,
};
// objects value can be accessed using period (.) operator.
const a10 = obj.a; // value of property a is accessed width (obj.a) and saved inside variable called a10
const b20 = obj.b;
const c30 = obj.c;
We can do this thing in one line with destructuring.
const {a, b, c} = obj // destructure obj property a,b,c and save it inside variable called a,b,c;
But we cannot change the variable name other than a,b,c, if we want to change it we have to do :-
const {a: a10, b: b20, c: c30} = obj // You can keep any name you want in place of a10, b20, c30 and access the value with the name you gave.
I hope you understood this explanation if not do let me know in comments.
code in video explained
Object named obj, containing properties a,b,c with value 10,20,30
const obj = {
a: 10,
b: 20,
c: 30,
};
// objects value can be accessed using period (.) operator.
const a10 = obj.a; // value of property a is accessed width (obj.a) and saved inside variable called a10
const b20 = obj.b;
const c30 = obj.c;
We can do this thing in one line with destructuring.
const {a, b, c} = obj // destructure obj property a,b,c and save it inside variable called a,b,c;
But we cannot change the variable name other than a,b,c, if we want to change it we have to do :-
const {a: a10, b: b20, c: c30} = obj // You can keep any name you want in place of a10, b20, c30 and access the value with the name you gave.
I hope you understood this explanation if not do let me know in comments.