#This object property doesn't want to update?

14 messages · Page 1 of 1 (latest)

hybrid saddle
#

I'm trying to change that property from a2 to a3:

const pieceKey = findPieceByPosition(allPieces, previousSelection);
const updatedPiece = {
...allPieces[pieceKey],
position: "a3",
};

alert(findPieceByPosition(updatedPiece, previousSelection)) /*undefined for some reason, thus no position: "a3"*/
const findPieceByPosition = (piecesArray, position) => {
for (const pieceKey in piecesArray) {
const piece = piecesArray[pieceKey]
if (piece.position == position) {
return piece
}
}
return undefined
}
quick flame
#
piecesArray.find(p => p.position === position)
#

you're returning the actual piece but then treating it as a key. stop using arrays like objects.

hybrid saddle
quick flame
#

yes

hybrid saddle
#

ok so if i got you correctly p is for position, and will it provide the property key instead of the property key as an object?

quick flame
#

...

hybrid saddle
#

let allPieces = {
w_pawn_1: { position: "a2", type: "pawn", color: "black", moved: "no" },

quick flame
#

you need to rewind and address my other comment first

#

dafaq? allPieces isn't even an array?

#

why would you name the parameter piecesArray when it's an object?

hybrid saddle
#

yeah i forgot to change the name from it from when it was an array

#

i think it used to be or something

#

my bad