Remove duplicates and gather secondary properties to a column

I have a table like this:

where it shows some duplicates under the property Note.
I am looking for a way to remove the duplication by moving the property of the duplicate to the property Relation as a part of a list, where Relation is a list itself.

I was able to find an approximate solution in StackOverflow and adapt it to my problem.
This is how originally the table looks:

And this is the final view after, gathering the rows, reordering, and removing duplicates by

Notice that the relation values have been grouped (gathered) in the final desired result.

This is the code:

```dataviewjs
const arr = [
  {'filename': "@Arzani2021",	'relation': "outlink"},
  {'filename': "@Arzani2021",	'relation': "inlink"},
  {'filename': "@Chen2018",	'relation': "outlink"},
  {'filename': "@Chen2018",	'relation': "inlink"},
  {'filename': "@PNNL2020RP",	'relation': "outlink"},
  {'filename': "@Raissi2017b", 'relation': "outlink"},
  {'filename': "@Raissi2017b", 'relation': "inlink"},
  {'filename': "@Rosofsky2021SL", 'relation': "outlink"},
  {'filename': "@Yin2021", 'relation': "inlink"},
  {'filename': "Collections", 'relation': "inlink"},
  {'filename': "Flow visualization", 'relation': "up"},
  {'filename': "Flow visualization", 'relation': "outlink"},
  {'filename': "PINNs collection", 'relation': "inlink"},
  {'filename': "cardiac flows", 'relation': "up"},
  {'filename': "cardiac flows", 'relation': "outlink"},
  {'filename': "fractional PDEs", 'relation': "related"},
  {'filename': "fractional PDEs", 'relation': "outlink"},
  {'filename': "high-speed flows", 'relation': "up"},
  {'filename': "high-speed flows", 'relation': "outlink"},
]

function collectDups(array, key) {
    const items = new Map();
    let cum = ""
    for (const obj of array) {
        const prior = items.get(obj[key]);
        let cum = obj["relation"]
        if (prior) {
            ++prior.occurrences;
            prior.relation = 
	            prior.relation.concat(", ").concat(cum)
        } else {
            items.set(obj[key], 
	            Object.assign({ occurrences: 1 }, obj))
	        
        }
    }
    // convert back to array form
    return [...items.values()]
}
	
let data = dv.array(collectDups(arr, 'filename'))
dv.table(["Note", "Relation", "Count"], data
    .sort(page => page.filename, "asc")
    .map(page => [
        dv.fileLink(page.filename),
        page.relation,
        page.occurrences
    ])
)

```

New to me are these lines:

  • const items = new Map(); : initialize a Map object
  • items.get(obj[key]): reads from the items map
  • items.set(): write over items map
  • Object.assign({ occurrences: 1 }, obj): assign the value of 1 to the key occurrences

These other lines:

  • ++prior.occurrences;: increments in one the counter occurrences
  • prior.relation: the gathering key in the object mapping
  • prior.relation = prior.relation.concat(", ").concat(cum): groups under one column all the type of relations where the note is found.

I got some inspiration from this post in SO:
node.js - Removing duplicates. generating new one and new key introduced on an array in javascript - Stack Overflow

There are several more ways to do this gathering operation. This one worked for me.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.