Collection problem

Is there any easy way to modify an array that is placed as a value in an Collection object?
6 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
XxX__Kamil_YT_XxX
XxX__Kamil_YT_XxXOP11mo ago
Im thinking about doing this:
const { Collection } = require("discord.js");
const collection = new Collection();
collection.set("xyz", ["1", "2", "3"]);

const query = "xyz";
const array = collection.get(query); // ["1", "2", "3"]
array.push("4");
collection.set(query, array);
const { Collection } = require("discord.js");
const collection = new Collection();
collection.set("xyz", ["1", "2", "3"]);

const query = "xyz";
const array = collection.get(query); // ["1", "2", "3"]
array.push("4");
collection.set(query, array);
but in an easier way
Squid
Squid11mo ago
You don't have to set() the key to the "new" value Arrays are just pointers in JS, so if you modify an array in one place, it modifies the array elsewhere too since both the array you modified and the array currently in the collection both point to the exact same memory address Other than that, you're doing it in the easiest way possible You could combine all of it into one line collection.get("xyz").push("4") but that isn't really changing anything under the hood
XxX__Kamil_YT_XxX
XxX__Kamil_YT_XxXOP11mo ago
thank you! One more question, would collection.get("xyz")["array"].push("4") work too?
Squid
Squid11mo ago
Looks like it
No description
XxX__Kamil_YT_XxX
XxX__Kamil_YT_XxXOP11mo ago
Tysm
Want results from more Discord servers?
Add your server