_mercury – 19-03 Jul 23

I have an array of objects like above ... suppose it has many objs So how to get the object with abbreviation en-US ? from the array by array accessing ? labels['en-US]` seems not to work .. it should be one line because i use it in EJS view engine
16 Replies
ScriptyChris
ScriptyChrisβ€’3y ago
Like this?
.filter(({ abbreviation }) => abbreviation === 'en-US')
.filter(({ abbreviation }) => abbreviation === 'en-US')
_mercury πŸ‡΅πŸ‡Έ
mmm Ok thx , i think i should remap the array of objs
Unknown User
Unknown Userβ€’3y ago
Message Not Public
Sign In & Join Server To View
_mercury πŸ‡΅πŸ‡Έ
yes
_mercury πŸ‡΅πŸ‡Έ
so how i convert this to an array to get the label by locals[abbreviation]
_mercury πŸ‡΅πŸ‡Έ
to be easy to access @ScriptyChris
ScriptyChris
ScriptyChrisβ€’3y ago
Use .find() instead of .filter() so it will give you first object, which has 'en-US' as abbreviation and then you can use object.abbreviation
_mercury πŸ‡΅πŸ‡Έ
I am thinking of restructure the obove obj
ScriptyChris
ScriptyChrisβ€’3y ago
What do you mean?
_mercury πŸ‡΅πŸ‡Έ
const attr_lbs_remapped = [];
attr_labels.map((label) => {
return (attr_lbs_remapped[label.abbreviation] = label.label);
});
const attr_lbs_remapped = [];
attr_labels.map((label) => {
return (attr_lbs_remapped[label.abbreviation] = label.label);
});
This does not work You may understand what i mean from the code Instead of array of objs It should be easy to be associative array ? Key : abbreviation, value : label
ScriptyChris
ScriptyChrisβ€’3y ago
I don't have idea what are you trying to do πŸ€” What structure do you want to have as an output?
_mercury πŸ‡΅πŸ‡Έ
thx for your active help I did it
const remapped_labels = {};
attr_labels.forEach((label) => {
remapped_labels[label.abbreviation] = label.label;
});
const remapped_labels = {};
attr_labels.forEach((label) => {
remapped_labels[label.abbreviation] = label.label;
});
if there is a better way , thx to tell
ScriptyChris
ScriptyChrisβ€’3y ago
This should work
const remapped_labels = Object.fromEntries(
attr_labels.map(({ abbreviation, label }) => [abbreviation, label])
)
const remapped_labels = Object.fromEntries(
attr_labels.map(({ abbreviation, label }) => [abbreviation, label])
)
_mercury πŸ‡΅πŸ‡Έ
Very good .. works
ScriptyChris
ScriptyChrisβ€’3y ago
Okay
reactibot
reactibotβ€’3y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1000478230899327086