#how to extract a value from a key:value pair inside of an object that's inside of an array

145 messages · Page 1 of 1 (latest)

novel wing

basically, i'm running a database querry that returns an array of results as an array and an object inside of the array for every matching result
it doesn't really matter that it's a databse though, that's not my concern, my concern is how to extract the values from a specific key for every single object that gets returned?
for example;

mighty owlBOT
  • 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!
tall jasper
novel wing

I haven't found how to extract a value from an object there though

tall jasper
novel wing
marble pastureBOT

Documentation suggestion for @novel wing:
mdn Array.prototype.filter()
The filter() method of Array instances creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

tall jasper

check if it‘s the right user using filter

then map the resulting array

more importantly though, you‘re running a database query, so if you‘re only interested in specific results, then you should modify your query to do that for you

novel wing
tall jasper

and where’s the problem? I‘m having trouble understanding what you‘re stuck on

what‘s the end result you want to get from the array of objects

novel wing

but i also need to reply with a specific message if the array is empty (aka, if the user has no strikes)

tall jasper

if you want to create one embedBuilder and append for each row of the array, there‘s also .reduce(), but I don‘t think that‘s even necessary in this case

novel wing
tall jasper

because that‘s how an array is stringified

novel wing

ah i see

tall jasper

Array.isArray(variable) can also show if it‘s an array

novel wing

yeah i know that but

tall jasper

it‘s general fuckery because console.log() is not the same as array.toString()

console.log() calls a formatting function

novel wing
tall jasper
novel wing

i console.logged the result before that and it logged []

tall jasper

essentially you're logging whether or not result and the newly created [] are stored at the same memory address, which is never possible

novel wing

ohhh okay that makes sense

one sec

tall jasper
novel wing

okay let me get back on track now

novel wing

cause i would be sending out how many strikes a specific person has

tall jasper
novel wing

if so then that's simpler than i thought lmfao

tall jasper

out of curiosity, is javascript your first programming language?

tall jasper

discord.js is pretty complex for a starter project

novel wing

it's not really my first project though

plus im kind of forced to learn like 4 programming languages at once so lmfao 😭

tall jasper

well, the length of an array exists in almost every programming language

so that's why I suspected it

novel wing

ah i see what you mean

novel wing

i always

tried to avoid fiddling around with complex array operations but

tall jasper
novel wing

yes i know that

okay hold on let me try something

novel wing

or actually i might just use fields

tall jasper
novel wing

this is purely me brainstorming but couldnt i make a for loop to make it add fields to the embed until it's at the last array index?

tall jasper
tall jasper
novel wing wdym

result array -> .map() to APIEmbedField {name: string, value: string, inline?: boolean} -> pass it to EmbedBuilder.addFields()

novel wing
tall jasper

my bad, value*

novel wing

or is this something else

right

novel wing
tall jasper
novel wing

i just

don't know what APIEmbedField is supposed to mean

im looking at the guide rn

tall jasper
novel wing

i can explain more if you misunderstood because i might've not explained it the best 😅

tall jasper

there's the upper limit of 25 due to how discord set that limit, but there's no limit for passing fields to the function

it uses rest parameters / spread operator

novel wing

like if i added 5 fields and someone only had 2 strikes for example

tall jasper

if you loop over an array and are out of bounds, then you wrote your for loop wrong.

novel wing

oh yeah about that pipeline

oh wait im supposed to be using a for loop

i moved it far out of the way lmfao one sec

novel wing

oh

novel wing
tall jasper

it can transform to ANY value. The function in map receives a strike object as parameter, and whatever you return ends up in the new array

that includes transforming the strike object into an APIEmbedField

If that confuses you, then I suggest looking up the following concepts

  • General: Callback Function
  • JavaScript: Arrow Function Syntax
novel wing
tall jasper

correct!

novel wing

meaning for me to add fields to an embed

it would add a new field

for every element in an array?

tall jasper

map is specifically meant for returning the function's value

it returns an array that has the same length as result, but each element within was changed according to the passed function

in this case, it's function(id) { return id.strike_id; }

novel wing

right so i need

ok so basically

what you're telling me i need to do is

since an object is a single element in an array

i need the function to extract the strike_id value from each object in an array, and then store it in a separate array

?

tall jasper

you need to take each strike object and change it into an APIEmbedField. Then you can return that newly created object

since you now have an array of APIEmbedField, you can pass it to EmbedBuilder.addFields()

novel wing

hmm wait i have an idea

novel wing
tall jasper

since strike_id is a string

novel wing

logged objewct

tall jasper

oh that

it‘s an array of strings

arrays are objects in javascript so typeof logged that

novel wing

ohhh

okay

OH okay i was

thinking of something else

novel wing

so

const newID = result.map((id) => id.strike_id);
                                const newReason = result.map((strike) => strike.strike_reason);
                                for (let i = 0; i <= newID.length; i++) {
                                        yesStrikes.addFields(
                                            {title: `${newReason[i]}`, value: `${newID[i]}`}
                                        )
                                    }
tall jasper

you can. There‘s always multiple ways

novel wing

ah

tall jasper

that code will fail though since i = newID.length is out of bounds

remember, 0 indexed. So i goes from 0 to newID.length - 1

novel wing

oh so

novel wing
tall jasper

yes

novel wing

OHH WAIT

i set it to title: not name:

i think

oh okay that fixed it, thank you so much for the time <3

I'll keep this open just in case I need something

I'll close it later today

novel wing

@tall jasper hey sorry for the ping but i have another question, i added a new column to the database which is date_issued which just stores a UNIX timestamp of when the command was ran.
Now when I try to print out those times on an embed using the same maping method as we described before, but it prints out the two values that are in the database for both cases, why is that?

const newID = result.map((id) => id.strike_id);
const newReason = result.map((strike) => strike.strike_reason);
const newTime = result.map((time) => time.date_issued)
for (let i = 0; i < newID.length; i++) {
  yesStrikes.addFields(
    { name: `${newReason[i]}`, value: `Strike ID: ${newID[i]} | <t:${newTime}>` }
  )
}

Sorry for the ping btw

never the fuck mind im blind as fuck

i swear to god i ask for help and as soon as i do it clicks

tall jasper

get a rubber duck so you can ask it first

if it clicks immediately, it‘ll work