#Array of collection to array
1 messages · Page 1 of 1 (latest)
First this is not channels map
It’s an array of (collection of guild members)
So first you need to convert collection of guild member to an array of guid members
yes
Then you need to combine
I would assume that involves
let arrayOfCollectionOfMembers = await myguild.channels.cache.filter(c => c.type === "GUILD_VOICE").map(c => c.members).reduce(... reduce stuff here ...);
so that would combine both together?
let arrayOfMembers = myguild.channels.cache.filter(c => c.type === "GUILD_VOICE").map(c => c.members).reduce((a, c) => [...a, ...c.values()], []);
a is the accumulator and c is current value in reduce function notation
so a is everything added to one and c is the current iteration
Yes
about to be added to the a
ait
so I would want to run in [] a + c.values() right?
because I want to accumulate the c.values()
You can combine two arrays by [...a, ...b] where a and b are two arrays
a and b can be any iterable to be exact
You can refer to the spread operator doc I recommend you earlier
let arrayOfMembers = await myguild.channels.cache.filter(c => c.type === "GUILD_VOICE").map(c => c.members).reduce((a,c) => [a + c.values()],[]);
so I wouldn't do the a + c like this?
wait the ... is actually part of the code
weird, ive never seen ... used before in code, when i usually do its people saying "i leave this blank for u to fill"
so everything explaining the ... is in the spread doc u put
so what is with the final box?
let arrayOfMembers = await myguild.channels.cache.filter(c => c.type === "GUILD_VOICE").map(c => c.members).reduce((a,c) => [...a, ...c.values()],[ ---this right here--- ]);
That's just empty array. The initial value for a for the first iteration
oh so that will be full of members after the reduce is finished?
Yes
I see
I also referred the reduce doc to you. You can see it for more details
so that turns the map objects of users into arrays, now do I need to put all the 4 arrays (4 voice channels in the server) into 1?
The code I gave will combine them into one Array
oh, thats nuts
can reduce take any more parameters or is it just the accumulator and current value?
I think it also takes index but not sure. You can refer the doc
ye
<:_:818272565419573308> Array.prototype.reduce()
The reduce() method executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
I understand most of it now, you explained it really well. I am still kinda confused on the ... syntax though. I am reading the spread document and I am just not sure what it does or why I would use it
Doc will clear it.