How to fetch data from a function?
This works, but I need to elaborate on it ...
I am sending a list of names to a function that sorts them in various ways and retrieving the name at the top of the list. The function has
I am stuck on the syntax for retrieving 3 names ... name1, name2 and name3
23 Replies
assuming a sorted array, you can do this:
array.slice(0, 3)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
there's also the horrible return [name[0], name[1], name[2]]
optionyes, i tried returning 3 names, but how do i "call" them?
are they functions?
doesn't work
i just want to put 3 names into 3 variables
no, but
let [name1, name2, name3] = ...
works
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignmentahhhhh, thank you
or, just use the array directly instead of passing the values to 3 variables
the array is sorted in many ways from many other functions
so needs its own function or i'm repeating code
depends
only seeing the code is when someone can answer that question
that didn't quite work, it took the third name and split it into the first 3 letters of that name
that's because you're returning a string, not the elements of the array
but it did work
otherwise, each variable wouldn't have the letters of the first name
calling just name1 returns a full name
you're returning just 1 name
in a string
you must do this too
can i not ?
no, javascript doesn't support multiple return values
ahhhhh
but you can do
return [name1, name2, name3]
and return an array of names, and break it this end ?
what do you mean with "and break it this end"?
i mean separate the names into individual variables
yes, that's the only way
oh i didn't need to - that does return 3 separate variables
thank you
you're welcome