#Array of collection to array

1 messages · Page 1 of 1 (latest)

fickle fog
#

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

lament plinth
#

yes

fickle fog
#

Then you need to combine

lament plinth
#

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 ...);
fickle fog
#

Yes

#

You can do both steps together

lament plinth
#

so that would combine both together?

fickle fog
#
let arrayOfMembers = myguild.channels.cache.filter(c => c.type === "GUILD_VOICE").map(c => c.members).reduce((a, c) => [...a, ...c.values()], []);
lament plinth
#

what does a and c mean in this situation?

#

im guessing C is channel

fickle fog
#

a is the accumulator and c is current value in reduce function notation

lament plinth
#

so a is everything added to one and c is the current iteration

fickle fog
#

Yes

lament plinth
#

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()

fickle fog
#

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

lament plinth
#
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?

fickle fog
#

Nope

#

That will lead to error

lament plinth
#

wait the ... is actually part of the code

fickle fog
#

Yes

#

It's the spread operator

lament plinth
#

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

fickle fog
#

It's an ES6 notation

#

But yeah it's widely used as placeholder for code too 😆

lament plinth
#

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--- ]);
fickle fog
#

That's just empty array. The initial value for a for the first iteration

lament plinth
#

oh so that will be full of members after the reduce is finished?

fickle fog
#

Yes

lament plinth
#

I see

fickle fog
#

I also referred the reduce doc to you. You can see it for more details

lament plinth
#

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?

fickle fog
#

The code I gave will combine them into one Array

lament plinth
#

oh, thats nuts

#

can reduce take any more parameters or is it just the accumulator and current value?

fickle fog
#

I think it also takes index but not sure. You can refer the doc

lament plinth
#

ye

sharp pebbleBOT
#

<:_: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.

lament plinth
#

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

fickle fog
#

Doc will clear it.