Question

How to achieve filtering effect similar to lodash differenceBy?

  • 22 October 2020
  • 2 replies
  • 109 views

I’m looking to possibly achieve a similar effect through Zapier formatter or filter to what lodash does with differenceBy utility function.

For example, having a trigger which comes with an object of following format:

{
current: {
labels: [{
id: '5f212d395422021ebc4b7041',
slug: 'old-label-1'
}, {
id: '5f212d395422021ebc4b7042',
slug: 'old-label-2'
},{
id: '5f212d395422021ebc4b7043',
slug: 'new-label'
}]
},
previous: {
labels: [{
id: '5f212d395422021ebc4b7041',
slug: 'old-label-1'
}, {
id: '5f212d395422021ebc4b7042',
slug: 'old-label-2'
}]
}
}


The effect of running differenceBy on this data would look like:

> _.differenceBy(data.current.labels, data.previous.labels, (i) => i.slug)
[ { id: '5f212d395422021ebc4b7043', slug: 'new-label' } ]


Would be awesome to achieve this through Zapier utilities otherwise I think the best next option would be having a separate trigger that only detects new labels?
Cheers!


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Nice tip @Troy Tessalone , thanks for pointing in a right direction!

I ended up with this solution for a data structure described above:
1. mapped `currentLabels` and `previousLabels` to input data from `#current.labels.slug` and `#previous.labels.slug`
2. Got this code running:

const currentLabels = inputData.currentLabels.split(',');
const previousLabels = inputData.previousLabels.split(',');

const addedLabels = currentLabels.filter(value => !previousLabels.includes(value));
const removedLabels = previousLabels.filter(value => !currentLabels.includes(value));

output = {
addedLabels,
removedLabels
};


A strange limitation I’ve found that isn’t well documented in Zapier. It’s around how they process every input as a string - every array is mapped in as comma separated string and object is serialized in a strange way where `JSON.parse()` can’t pick it up (throws parsing errors that are a nightmare to debug). But got it to do what I wanted in the end :grin:  Hopefully this helps to someone looking solve a similar problem in the future.

Userlevel 7
Badge +14

You’d have to use a Code step to do this type of comparison: https://zapier.com/apps/code/help