Problems to acess objects inside an array

Hey guys, im trying to make a game with solid js using it as my interface library, but im having this weird behavior on createStore signal. I have this store: export const [spaceShip, setSpaceShip] = createStore({ name:"Galahdyx 5C", inventory:{ resources:[] }, currentGasoline:100, maxGasoline:100, shield:10, laser:10, population:6 }) im trying this little thing: When i click on a button, i append an object with this interface on the resources array: { Name: string Quantity: Number } im trying to do that with this function: setSpaceShip("inventory", "resources", resources => { const existingResource = resources.find(resource => resource.name == picketResource); if(existingResource){ return resources.map(resource => { resource.name == picketResource ? {...resource, quantity: (resource.quantity || 0) + 1} : resource }) }else{ return [...resources, { name: picketResource, quantity:1 }] } }); What im doing wrong?
3 Replies
TaQuanMinhLong
I think you can fix your resource type to Record<string, number> instead Then if you need to get all in inventory, run a for in loop (loop keys) to get all resource So for adding inventory, just simply
setSpaceShip("inventory", "resource", resource.name, resource.quantity)
setSpaceShip("inventory", "resource", resource.name, resource.quantity)
Madaxen86
Madaxen866d ago
Have a look at this guide. With produce you‘ll simplify your code. https://docs.solidjs.com/guides/complex-state-management#adding-to-an-array
Lightsyr (Leandro Campos)
Thank you so much, now it works perfectly!
Want results from more Discord servers?
Add your server