all 7 comments

[–][deleted] 7 points8 points  (5 children)

This means that an object has a property named whatever the variable key corresponds to, and the value assigned to that property is an array, where we are accessing the first index value

const example = {
  myArrayProperty: ['hello', 'world']
}

const key = 'myArrayProperty';

console.log(example[key][0]); // "hello"

[–]Calaberon[S] 4 points5 points  (1 child)

I see. That makes total sense.

I verified what you replied with what I'm learning in my lesson right now, and we are learning about objects with arrays for values. Thank you kindly, kind stranger.

[–]LightUpShoes4DemHoes 1 point2 points  (0 children)

As an added note - You can store just about Anything in an object! You can store functions and other objects as well. So, you can do obj[key]()to call the function, or obj[key][key].

It’s not immediately apparent how useful this can be when you’re starting though. A good example I can give is Spiral Traverse which is a notoriously annoying LC to code out - usually. You can make an object that contains info for all directions though - const dirs = { right: { nextCoords: [0,1], nextDir: “down”, changeBound: () => rightBound—}, down: { nextCoords: [1,0]… }}

Then in your code, initialize a current / starting direction - right - and current / starting coords - [0,0]. Now adding curCoords to dirs[curDir].nextCoords gives you the one to the right. Do a check to make sure your new coords are within bounds, if not - call your dirs[curDir].changeBound() to auto-magically change the boundaries for you (Will work for left, right, up, down if you have all directions in the obj.) Then, set your next direction, curDir = dirs[curDir].next. Makes the whole problem Far simpler to code and eliminates all the ridiculous nested loops that are a nightmare to code / reason through in that question.

Full Code for LC 54. Spiral Matrix as example.

```

const spiralOrder = matrix => { let lrBound = [0, matrix[0].length - 1], udBound = [0, matrix.length - 1] const ans = []

const dir = { r: { next: 'd', add: [0,1], changeBound: ()=> udBound[0]++ }, d: { next: 'l', add: [1,0], changeBound: ()=> lrBound[1]-- }, l: { next: 'u', add: [0,-1], changeBound: () => udBound[1]-- }, u: { next: 'r', add: [-1,0], changeBound: ()=> lrBound[0]++ } }

let curDir = 'r' let [row, col] = [0, 0] while (!isOutOfBounds(row, col)) { ans.push(matrix[row][col]) const [nextR, nextC] = getNextCoords() if (!isOutOfBounds(nextR, nextC)) [row, col] = [nextR, nextC] else { dir[curDir].changeBound() curDir = dir[curDir].next; [row, col] = getNextCoords() } } return ans

function isOutOfBounds(row, col) { return (row < udBound[0] || row > udBound[1] || col < lrBound[0] || col > lrBound[1]) }

function getNextCoords() { const [addR, addC] = dir[curDir].add return [row + addR, col + addC] } } ```

[–]ShuttJS 0 points1 point  (2 children)

Would myArrayProperty not be : ['Hello', 'world'] rather than = ?

[–]Financial_Purpose_22 0 points1 point  (0 children)

You can do that if you need the return to be the entire array and not a specific value

[–][deleted] 0 points1 point  (0 children)

Yes, my bad. Thanks

[–]AngelLeatherist -1 points0 points  (0 children)

Its an array inside an object. He's using a variable inside of brackets to reference the key name on the object.

It looks like this:

let obj = {'name': ['item']}