Hey there,
I would love a bit of help with an issue I have been struggling with:
I have large data set containing keywords / descriptive tags to describe/classify the designs on our webshop juniqe.com, they are stored on a table called Image Keywords in my base, whereas the designs they are related to are on a table called DESIGNS
I wrote the script below to merge the duplicate entries in the keyword table, since these records hold much information other than the linked record to DESIGNS. I’d like to believe that the script does the following
- cycle through all the records and compare the current records main field to the others, using the field Name (EN) as comparison value (the keywords english translation)
- If a duplicate has been found, combine the linked records of both the instances the script has found, and write them back to the first instance
- Clear the linked record field of the second instance (the comparison record) and mark that record with a checkbox field called DUPE to indicate a duplicates. My intention was to clear the duplicates later manually after having reviewed and verified the script works.
The code does do in parts what it’s supposed to do - it certainly calculates and runs forever.
However, in the end, it produces some unexpected results with records that are marked as Duplicates but still contain records in the linked record field.
So, something doesn’t work as expected, I have a feeling I am missing a trivial but essential logical element of the loop, but by god, I can’t figure it out.
This is the code, any help appreciated, it seems to combine and reconstruct the arrays correctly but i feel the way the two loops are designed must be flawed somehow:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// GET TABLES AND DEFINE VARIABLES
let table = await base.getTable("Image Keywords");
let query = await table.selectRecordsAsync({fields:{"Designs - Vision AI","Name (EN)","DUPE"]})
let lower ="l";
let id = "123";
// OUTER LOOP -> LOOP THROUGH ALL KEYWORDS, GET CURRENT KEYWORD
for (let count = 0; count < query.records.length; count++) {
// GET THE TRIMMED LOWER CASE KEYWORD AND RECORD ID
lower = query.records.count].getCellValueAsString("Name (EN)").toLocaleLowerCase().trim();
id = query.records.count].id
// INNER LOOP -> COMPARE CURRENT KEYWORD TO THE REST OF THE ARRAY
for (let countlower = count+1; countlower < query.records.length; countlower++) {
// IF OF RECORD OF INNER LOOP MATCHES THE CURRENT KEYWORD, ISN'T THE SAME RECORD AND HASN'T PREVIOUSLY MARKED AS DUPE, ESTABLISH A DUPLICATE HAS BEEN FOUND
if (lower==query.records.countlower].getCellValueAsString("Name (EN)").toLocaleLowerCase().trim() && id!=query.records.countlower].id && query.records.count].getCellValue("DUPE")!=true && query.records.countlower].getCellValue("DUPE")!=true) {
console.log("dupe ",lower,"-",query.records.countlower].getCellValueAsString("Name (EN)").toLocaleLowerCase().trim())
let designarray = query.records.countlower].getCellValue("Designs - Vision AI")
let designarraysec = query.getRecord(id).getCellValue("Designs - Vision AI")
console.log(designarray,designarraysec)
// COMBINE LINKED FIELDS OF BOTH RECORDS, DEPENDING ON WHICH ONE OF THEM MIGHT BE EMPTY
if (designarraysec!=null && designarray!=null){designarray = designarray.concat(designarraysec)}
if (designarray==null && designarraysec!=null){designarray = designarraysec}
if (designarray!=null && designarraysec==null){}
// IF BOTH ARE EMPTY, MARK INNER ARRAY'S DUPLICATE RECORD AS DUPE=TRUE
if (designarray==null && designarraysec==null){
console.log("no records")
await table.updateRecordAsync(query.records.countlower].id,{
"DUPE":true
});
}
else {
// IF EITHER ARRAYS ARE NOT EMPTY, RE-CONSTRUCT THE COMBINED ARRAY...
console.log (designarray)
let newarray = new Array();
let unique = n...new Set(designarray.map(keyword => keyword.id))];
for(let record of unique) {
newarray.push({"id":record})
}
// ...THEN UPDATE THE OUTER LOOP'S RECORD WITH THE COMBINED ARRAY ...
await table.updateRecordAsync(id,{
"Designs - Vision AI":newarray
});
// ...AND UPDATE THE INNER LOOP'S DUPLICATE TO SET DUPE=TRUE AND CLEAR ALL LINKED RECORDS
await table.updateRecordAsync(query.records.countlower].id,{
"Designs - Vision AI":i],
"DUPE":true
});
}
}
}
}```