R
Reactiflux

_mercury – 19-03 Jul 23

_mercury – 19-03 Jul 23

M_mercury7/23/2022
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
SScriptyChris7/23/2022
Like this?
.filter(({ abbreviation }) => abbreviation === 'en-US')
.filter(({ abbreviation }) => abbreviation === 'en-US')
M_mercury7/23/2022
mmm Ok thx , i think i should remap the array of objs
UUUnknown User7/23/2022
Message Not Public
Sign In & Join Server To View
M_mercury7/23/2022
yes
M_mercury7/23/2022
so how i convert this to an array to get the label by locals[abbreviation]
M_mercury7/23/2022
to be easy to access @ScriptyChris
SScriptyChris7/23/2022
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
M_mercury7/23/2022
I am thinking of restructure the obove obj
SScriptyChris7/23/2022
What do you mean?
M_mercury7/23/2022
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
SScriptyChris7/23/2022
I don't have idea what are you trying to do 🤔 What structure do you want to have as an output?
M_mercury7/23/2022
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
SScriptyChris7/23/2022
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])
)
M_mercury7/23/2022
Very good .. works
SScriptyChris7/23/2022
Okay
UUUnknown User7/24/2022
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

_mercury – 19-03 Jul 23

Join Server