I am rather new to Javascript. I am trying to run following code which I googled so that could be problem #1. The code is suppose to read a list which is created in one of my columns and to extract unique values only.
It does not return any results. Can any one please tell me what I am doing wrong?
let arr = [p1];
let unique = [];
arr.forEach(function (element) {
if (!unique.includes(element)) {
unique.push(element);
}
});
return unique;
The javascript column doesn’t return arrays. You need to first convert the array into a string. Once you return a text string, then you can convert it to an array using a Split Text column.
Also, are you trying to pass an array or text into the p1 parameter? If it’s an array, you may need to first convert it to a comma delimited string using a Joined List column, and then perform a Split in the javascript code to split it into an array.
The javascript column is only really good at passing text strings in and out.
Hi Jeff. Thank you for your reply. I am now getting a result. But the result still included duplicates. So I guess the function is not working properly.
As to you question - My p1 parameter is a Joined List column.
This is what my code looks like after the change:
let arr = p1.split(',');
let unique = [];
arr.forEach(function (element) {
if (!unique.includes(element)) {
unique.push(element);
}
});
return unique.join(',');
This is the p1 parameter value - duplicates marked in bold: CN6-CI612, CN5-CI512, CN6-CI612, CN6-CI632, CN5-CI512, CN5-CI532
This is my output: CN6-CI612, CN5-CI512, CN6-CI612, CN6-CI632, CN5-CI532
So it appears to be mostly working. Your code looks good. Do you maybe have any extra spaces in one of your ‘CN6-CI612’ items? You could maybe try trimming ‘element’ in the IF and when you do the Push.
Actually, it might be the space. The first element does not have a leading space, but the rest do.