MARTIN GITAU
MARTIN GITAU

Reputation: 81

wrong conversion of a string to a variable

kindly assist me to know why when i call a function and pass a parameter using a string in its 'raw form' i get the right output but when i assign it to a variable first i get a different output.

i want to convert CSV string into a JavaScript array of arrays. when i pass the parameter in console in its raw form i.e console.log(relay('a;b\nc;d', ';')) i get the right results which is an array but when i convert the string into a variable called dat i get the wrong array [";"] which is basically the second part of my string.

relay = (dat, delimiter = ',', tested = false) =>
  dat
  .split('\n')
  .slice(tested ? dat.indexOf('\n') + 1 : 0);
let dat = ('a;b\nc;d', ';');
console.log(relay(dat)); // [";"]
console.log(relay('a;b\nc;d', ';'));

When i call the function using dat variable I get the output as [";"]

When I call the function as console.log(relay('a;b\nc;d', ';')) I get the right output  ["a;b", "c;d"]

the result i expect is an array of arrays [["a;b"], ["c;d"]]

Upvotes: 2

Views: 91

Answers (4)

MARTIN GITAU
MARTIN GITAU

Reputation: 81

i have noticed that the default parameter is also being passed with the comma operator, i.e the string is not passed as a whole ('a;b\nc;d', ';');

Upvotes: 0

Gershom Maes
Gershom Maes

Reputation: 8170

You've defined dat:

let dat = ('a;b\nc;d', ';');

You should try to print dat, so you can see what it looks like:

console.log(dat);

You will see that dat is ';' - this is because of the relatively obscure comma operator, which simply selects the value after the last comma. In your case, ';' occurs after the last comma.

Upvotes: 1

Barmar
Barmar

Reputation: 782099

This line

let dat = ('a;b\nc;d', ';');

does not do what you think. It's using the comma operator, which just returns its right operand, so it's equivalent to

let data = ';';

You could assign an array to the variable, and then spread it in the call.

relay = (dat, delimiter = ',', tested = false) =>
  dat
  .split('\n')
  .slice(tested ? dat.indexOf('\n') + 1 : 0);
let dat = ['a;b\nc;d', ';'];
console.log(relay(...dat));
console.log(relay('a;b\nc;d', ';'));

Upvotes: 4

Wei Seng Tan
Wei Seng Tan

Reputation: 507

function log(s1, s2) {
  console.log(s1, s2);
}

let str1 = 'a';
let str2 = 'b';
log(str1, str2); // 'a' 'b'

let str = ('a', 'b');
console.log(str); // 'b'
log(str); // 'b' undefined

Notice that you cannot assign two value to one variable and use it to pass as arguments to a function. To achieve what you want, you can either use two variables, one for dat and one for delimiter, call the function with these two variables.

let dat = 'a;b\nc;d';
let delimiter = ';';
console.log(relay(dat, delimiter));

Another way is to use an object with multiple properties, e.g.

relay = ({ dat, delimiter = ',', tested = false }) => {
  dat
    .split('\n')
    .slice(tested ? dat.indexOf('\n') + 1 : 0);

  return dat;
}

let config = {
  dat: 'a;b\nc;d', 
  delimiter: ';'
};
console.log(relay(config));

Upvotes: 1

Related Questions