I'm 100000% new to the ternary world, so I really don't understand what's going on. I have the following:
categories.forEach((category) => {
const errorCategory = {
[category]: response.map(
(r) => r.ds_category === category && r.ds_name
),
};
errors = { ...errors, ...errorCategory };
});
I want to filter the r.ds_name to remove all "false" results, how could I do this? I tried:
Doing the filter straight away (r.ds_name.filter(blablabla)) but I get a "this is not a function" error.
Switching from ternary to the old school function, and didn't get any results either.
Filtering errorCategory (errorCategory = errorCategory.filter((item) => item === "false"), but I also got the "this is not a function" error.
Thanks :(
To filter an array, I'd recommend using '.filter' instead of '.map'. This will remove the elements which do not match the requirement set. Assuming response is an object which is an array, or other object which implements 'filter', you can do the following (note the extra brackets):
categories.forEach( ( category ) => {
const errorCategory = {
[ category ]: response.filter(
(r) => (r.ds_category === category && r.ds_name)
),
};
//Do stuff
}