Hello, it's not often that i write here, but it seems i have a problem, which makes me crazy!
What i am talking about?
I am trying to generate a specific code to get UNIQUE-DATA out of DATABASE using Wix-Data.
It works!
async function create_UniqueDropdown(items, DBFIELDS, dropdown) {console.log(items)
const uniqueTitles = await getUniqueTitles(items); console.log("Unique-Titles: ", uniqueTitles);
$w(`#${dropdown}`).options = buildOptions(uniqueTitles); //console.log("OPTIONS: ", buildOptions(uniqueTitles));
async function getUniqueTitles(items) {
let titlesOnly = await items.map(item => item[DBFIELDS]); //console.log("Titles-Only: ", titlesOnly);
return [...new Set(titlesOnly)];
}
function buildOptions(uniqueList) {
return uniqueList.map(curr => {
return {label:curr, value:curr};
});
}
}
But my aim is to modify it the way, that the user can choose the exact DB-field to be unified and get all results of the selected field(s).
At least DBFIELDS must be an ARRAY, or even an OBJECT.
The shown code runs good for just one DB-Field, but would quickly get very slow when working for several DB-fields.
So my idea was to use promiseAll(), but this is exactly my issue, no matter what i tried, i could not get a satisfying result.
Maybe one of you have a good idea?
Aim-1: --> eleminating all --> asynchronity (async-await)
Aim2: ---> modifiying this function into a multi-DB-FIELD one.
So the Input for DBFIELDS would be an ARRAY for example...
DBFIELDS = ["dbField1", "dbField2", "dbField3", ......]
And as result would be expected all UNIQUE-ITEMS for each of the DB-FIELDS --> compressed into an ARRAY or Object.
Before I answer, do you need the unicity to be on all fields combined or unique within each field? In other words is an item unique if valueField1 or valueField2 is unique or if the combination of valueField1+valueField2 is unique? If it's the latter you can do something like
function getUniqueItemsForField(items, fields){ const itemsByKey = {} items.map(item => { const key = fields.reduce((compoundKey, field) => compoundKey+field+":"+item[field]+"-", ""); if(!itemsByKey[key]) itemsByKey[key] = item }) return Object.values(itemsByKey) }