So just a quick questions since I'm relatively new to javascript.
Let's say I have
const {a, b, c, d} = items[i]
and I have a list of items somewhere in the document. What would this code render? What does it essentially stand for? I know how to define multiple constants in one line but I get confused when you incorporate the index (i). Any explanation would help. Thank a ton!
{a, b, c, d} means you are destructuring the properties of an object. In this case you are destructuring to define the const variables a,b,c,d.
items is built with multiple objects. So we destructure the items[2] object properties to define the const variables.
const items = [
{a: 0, b: 0, c: 0, d:0},
{a: 1, b: 1, c: 1, d:1},
{a: 2, b: 2, c: 2, d:2}
]
const {a, b, c, d} = items[2]
console.log(a) // <--- outputs 2
You can also destructure the array by doing something like so:
const items = [
{a: 0, b: 0, c: 0, d:0},
{a: 1, b: 1, c: 1, d:1},
{a: 2, b: 2, c: 2, d:2}
]
const [obj1, obj2, obj3] = items
console.log(obj1) // <--- outputs {a: 0, b: 0, c: 0, d:0}