const data = { name: "Test", skills: ["javascript", "css", "react", "node"] };
// Extract 1st item
const [firstSkill] = data.skills;
console.log(firstSkill); // javascript
// Extract 3rd item (Skip 1st & 2nd)
const [, , thirdSkill] = data.skills;
console.log(thirdSkill); // react
// Nested destructuring (Direct from object)
const {
skills: [firstSkillAgain],
} = data;
console.log(firstSkillAgain); // javascript
// Assign default value if nested array not found
const { tags: [firstTag] = ["Custom Value 1", "Custom Value 2"] } = data;
console.log(firstTag); // Custom Value 1
Thanks for reading 💙
Follow @codedrops.tech for daily posts.
Instagram ● Twitter ● Facebook
Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript
codedrops.tech
Top comments (2)
Destructuring is a very cool technique 👌
Agree