Skip to content

Instantly share code, notes, and snippets.

@rmdes
Forked from agizis/deduplicate.js
Last active March 10, 2023 18:09
Show Gist options
  • Save rmdes/5de3387688e9e8acac97ae25c2ad320c to your computer and use it in GitHub Desktop.
Save rmdes/5de3387688e9e8acac97ae25c2ad320c to your computer and use it in GitHub Desktop.
const new_items = [];
const key_field = "guid";
console.log("key_field from json.key: " + key_field);
const now = $now
// Get static data stored with the workflow
data = $getWorkflowStaticData("node");
// delete the old cache, that didn't have time
data.ids = null;
// new cache of id => time added
data.seen = data.seen || {};
for (let i = items.length - 1; i >= 0; i--) {
var item = items[i]
if(item.json[key_field]){
console.log("item: " + item.json[key_field])
// Check if data is already present
if (!data.seen[item.json[key_field]]) {
// if new data then add it to an array
new_items.push(item);
data.seen[item.json[key_field]] = now
}
} // errors have no guid, just drop 'em
}
// destroy everything older that 24 hours from data.seen
var yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));
const keys = Object.keys(data.seen)
for (var key in keys){
var orig_date = data.seen[key];
if (orig_date < yesterday){
console.log("removing old item: " + key + " from " + orig_date)
data.seen.delete(key);
}
}
// return new items
return new_items;
@rmdes
Copy link
Author

rmdes commented Mar 10, 2023

This is a JavaScript code snippet that seems to filter an array of objects (items) based on a unique identifier (key_field) and a timestamp.

First, it declares a new array new_items and a constant key_field that is set to "guid". Then, it retrieves some data stored in the workflow using $getWorkflowStaticData("node"). It then proceeds to delete an old cache and initializes a new one (data.seen) as an empty object.

Next, it loops through each object in the items array in reverse order, starting from the last item, and checks if it has a value for the key specified in key_field. If it does, it checks whether the value of this key is present in the data.seen cache. If it is not present, it adds the item to the new_items array and updates the data.seen cache with the new key and the current time. Otherwise, it skips the item.

After the loop, it deletes all the keys in data.seen that are older than 24 hours. Finally, it returns the new_items array.

Note that some parts of the code are specific to the environment or platform where it is being run. For example, the $now variable may be specific to the platform or library being used, and the $getWorkflowStaticData() function may also be specific to the workflow engine or platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment