Remove Array Duplicate using javascript

var Arr = [];

for (var x = 0; x < ${Arr_CSV}.length; x++) {
  var Exists = "Y";
  for (var y = 0; y < Arr.length; y++) {

    if (${Arr_CSV}[x] == Arr[y]) {
      Exists = "N";
      break;
    }
  }

  if (Exists === "Y") {
    Arr.push(${Arr_CSV}[x]);
  }
}

return (Arr);

There are bugs with various javascript commands so I used this logic.
but duplicates are not removed.
Is it also a bug?

In javascript to compare values need == or === try to edit your command and retry again if works

same code works without using array variable.

Waititing for a javascript expert, I know little about javascript

most of the commands gives this error

Unexpected token (5:3)

{
  "Name": "aaaaaaaaa",
  "CreationDate": "2023-6-6",
  "Commands": [
    {
      "Command": "csvReadArray",
      "Target": "test.csv",
      "Value": "csv",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "duplicate: ${csv}",
      "Value": "red",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "return [...new Set(${csv}.flat())]",
      "Value": "aa",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${aa}",
      "Value": "green",
      "Description": ""
    }
  ]
}

image

strange
when I was trying I got errors
Unexpected token

It worked but gave error again

tried with both
executeScript_Sandbox
and
executeScript

run after some code
you will get this bug

function uniq(arr) {
  // Flatten the array
  var flattenedArr = [].concat.apply([], arr);

  // Create an empty object to store unique values
  var uniqueObj = {};

  // Iterate over each element in the flattened array
  for (var i = 0; i < flattenedArr.length; i++) {
    var value = flattenedArr[i];

    // Check if the value is already in the uniqueObj
    if (!uniqueObj[value]) {
      uniqueObj[value] = true;
    }
  }

  // Extract the keys of uniqueObj into a new array
  var uniqueArr = Object.keys(uniqueObj);

  // Convert the keys from strings to actual values
  var resultArr = uniqueArr.map(function (key) {
    return Number(key);
  });

  return resultArr;
}

return uniq(${csv})

converted to es5 using chatGPT. executeScript_Sandbox probably works now

That seems to indicate to me that the variables you are using are not in a proper Array format.
Are you able to use the “foreach” kantu command on you your ArrayVariable? If this is an problem then you kanu variable is not an array. Maybe the content looks like an array but is a string.

{
      "Command": "csvReadArray",
      "Target": "Test.csv",
      "Value": "csv",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${csv}",
      "Value": "red",
      "Description": ""
    },
    {
      "Command": "forEach",
      "Target": "csv",
      "Value": "a",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${a}",
      "Value": "green",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
[echo]
1,2,3,4,1
[echo]
1
[echo]
2
[echo]
3
[echo]
4
[echo]
1

No I get this output

[echo]

NaN,NaN,NaN,

Its not working with strings in CSV but only numbers.

changed to

if (!uniqueObj[value] && typeof value === 'number' && !isNaN(value)) {
  uniqueObj[value] = true;
}

but now get error “Unexpected token”

i think its bug with javascript commands.

function uniq(arr) {
  // Flatten the array
  var flattenedArr = [].concat.apply([], arr);

  // Create an empty object to store unique values
  var uniqueObj = {};

  // Iterate over each element in the flattened array
  for (var i = 0; i < flattenedArr.length; i++) {
    var value = flattenedArr[i];

    // Check if the value is already in the uniqueObj
    if (!uniqueObj[value]) {
      uniqueObj[value] = true;
    }
  }

  // Extract the keys of uniqueObj into a new array
  var uniqueArr = Object.keys(uniqueObj);

  return uniqueArr;
}

return uniq(${csv})

ok this is working for now