Hello n8n community! This is a general question, and I have no problems – but I’m curious
I often do data transformations with the Function node. An example can be this code (this is a WIP so please don’t look at the variable naming ):
const parameterArray = [];
const data = $items("data");
data.map(category => {
if (category.json.parameters.length > 0) {
category.json.parameters
.map(p => parameterArray.push({
json: p
}))
}
})
const uniqueArray = parameterArray
.filter(p => p.json.values.length > 0)
.filter(function (item, pos, self) {
return self.indexOf(item) == pos;
});
const valueArray = [];
uniqueArray.map(parameter =>
parameter.json.values.map(value => valueArray.push({
json: {
value: value,
parameterId: parameter.json.parameterId
}
}))
)
return valueArray;
I’m a javascript beginner, and I’d like to know if there’s an efficient way to write little functions like this one, especially in the context of n8n. Or if you have any comments related to this code, I’d be happy too.
Thanks for the inspiration!
Honza