sei-ec-remote / project-1-issues

Open new issues here
1 stars 2 forks source link

Issue using array.find() #111

Closed zyaffee closed 1 year ago

zyaffee commented 1 year ago

What's the problem you're trying to solve?

Trying to assign a variable using find() but it returns undefined

Post any code you think might be relevant (one fenced block per file)

// this is part of a function called when a DOM element is clicked
let mapIdString = e.target.getAttribute('id') // gets an xy string from element, works as intended
let coordinates = [parseInt(mapIdString[0], 10), parseInt(mapIdString[3], 10)] // assigns relevant info from above into array of ints, works as intended
let territoryObj = gameMap[1].find(territory => {territory.mapId === coordinates}) // THE PROBLEM, gameMap[1] is an array of territory objects, each with an array property called mapId containing ints, I call find on the big array looking for the element.mapId that matches the coordinates but it's always undefined so far

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

no error, but there will be if this isn't defined

What is your best guess as to the source of the problem?

I am not using find correctly

What things have you already tried to solve the problem?

I've read the docs on find a couple times, I've tried explicitly returning the element with an if, I've tried several variations on the above syntax, I've tried strict and loose equality for the condition. I've console logged my outputs and the arrays that are supposed to match do seem to match, they both log as " Array [ x, y ] " in my inspector

Paste a link to your repository here

https://github.com/zyaffee/Baraka

kestler01 commented 1 year ago

please add a brief summary of the solution to the thread and close it for us so we know it's all set ! 😁

zyaffee commented 1 year ago

solution: arrays in Javascript cannot be compared using == or ===, this operation will always return undefined. the solution is to compared the values at like indices of arrays instead.