function removeItemFromDynamicProperty(player, number) {
world.sendMessage(number)
const dy = player.getDynamicProperty('backpack');
if (!dy) {
world.sendMessage("Backpack is empty or not defined.");
return;
}
const backpack = JSON.parse(dy);
const { slots, items, amounts, randoms } = backpack;
world.sendMessage(`Before: ${slots} /// ${items} /// ${amounts} /// ${randoms}`);
const index = slots.indexOf(number);
if (index === -1) {
world.sendMessage(`Item with number ${number} not found in backpack.`);
return;
}
slots.splice(index, 1);
items.splice(index, 1);
amounts.splice(index, 1);
randoms.splice(index, 1);
const updatedBackpack = { slots, items, amounts, randoms };
world.sendMessage(`After: ${slots} /// ${items} /// ${amounts} /// ${randoms}`);
player.setDynamicProperty('backpack', JSON.stringify(updatedBackpack));
}
#why is my filter not working?
1 messages · Page 1 of 1 (latest)
[2024-05-28 20:02:07:988 ERROR] [Scripting] TypeError: cannot read property 'indexOf' of undefined
yes
You’re passing it into the function as a string
yes
The stringify is then taking that as a string
And it is being read as a string
Ah, also; you’re saving the data as an array of objects
But you’re reading the data as just an object
how
Here it’s being saved as an array of objects
And here you’re destructuring it, but it’s being saved as an array, so all the variables in the destructuring are undefined
how can I define them suddenly?
In that case read it as an array here
I could have an example I really don't understand what you mean
Console warn your saved data
yes
So an array of objects
Yet in here, you are reading the data like it is not an array, and just a single object
yes
That does not work
I still have this error
Because you are attempting to destructure an array of objects as though it is just an object
That would be this problem
?
Wdym
how can I remove all the line now?
Remove what line
a line which includes the number that I choose is the number that and in the line I want to recover and supp all the line c possible? if yes how can I do it
I do not understand
I want to delete a specific line in my dynamicprop