const army = {
name: 'karan',
memb : [
{
captain: 'suraj',
},
{
rifleMan: 'sanjay'
},
{
grenadier: 'yogesh'
}
]
}
const { name , memb:[, cap]} = army
console.log("cap ", cap)
This gives cap { rifleMan: 'sanjay' }
.
Can anyone please explain this concept and what it is called
You are de-structuring the army
object. So "karen"
is stored in name
and from the army.memb
which is an array you want the second element and store it in cap
.
For example if you put two commas it means you want the third element of the memb
array:
const army = {
name: 'karan',
memb: [{
captain: 'suraj',
},
{
rifleMan: 'sanjay'
},
{
grenadier: 'yogesh'
}
]
}
const { name, memb: [,, cap]} = army
console.log("cap ", cap)
It is feature of array destructuring
in javascript. It is used to ignore or skip certain values while destructuring.
const [a, , b] = [1, 2, 3];
console.log(a,b); // 1,3
const [, a, b] = [1, 2, 3];
console.log(a,b); // 2,3