0

Trying to convert comma-separated string to array w/ quotes around each value in js:

var ipd = 'field1,field2';
console.log('Img Pad string: ' + ipd); // field1,field2
var ipdarray = new Array();
ipdarray = ipd.split(',');
console.log('Img Pad array split: ' + typeof ipdarray); // object
console.log('Img Pad array: ' + ipdarray); // field1,field2

I want the array to be: ["field1","field2"]

Tried to follow this: Convert comma separated string to array but it's not converting to an array.

Daniel Pérez
  • 1,873
  • 1
  • 17
  • 25
parti
  • 205
  • 3
  • 15

3 Answers3

3

It is being converted to an array, but when you output the string (in console.log), the array is being coerced back into that string syntax because of the + operator (acting as a concatenator).

Here is a more isolated example of what is happening:

var string = 'field1,field2';
var arr = string.split(',');
console.log(Array.isArray(arr));
console.log(arr.length);

//When using the +, it will coerce into a string
console.log('Coerce to string ' + arr);

//When passing the array as an argument, it will stay an array
console.log('Stays and array', arr);

In order to preserve the array literal syntax, you'll need to JSON.stringify the array before outputting it if you want to use the + operator, or you can just output the array as a separate argument to console.log

JSON.stringify(ipdarray);

See it in action:

var ipd = 'field1,field2';
console.log('Img Pad string: ' + ipd); // field1,field2
var ipdarray = new Array();
ipdarray = ipd.split(',');
console.log('Img Pad array split: ' + typeof ipdarray); // object
console.log('Img Pad array: ' + JSON.stringify(ipdarray)); // ["field1","field2"]
// OR
console.log('Img Pad array: ', ipdarray); // ["field1","field2"]

Also, arrays are "object"s, but there are other ways to determine if that "object" is an instance of an array:

var arr = [];
console.log(typeof arr);
console.log(arr instanceof Array)
console.log(Array.isArray(arr));
KevBot
  • 17,900
  • 5
  • 50
  • 68
  • but this is not an array, is a string – Daniel Pérez Jun 05 '17 at 01:23
  • @DanielPérez, what do you mean? I am using the code snippet provided by the OP. It was converted to an array, but when console logging with the + operator, it converts the array to a string – KevBot Jun 05 '17 at 01:26
  • @KevBot - Thanks for the explanation, much appreciated – parti Jun 05 '17 at 01:27
  • I mean that `JSON.stringify` return a string, despite that looks like an array of string, it is not, I thought that the OP wanted an array of string surrounded by `"`, like `['"field1"','"field2"']` – Daniel Pérez Jun 05 '17 at 01:28
0

Given input string you can use String.prototype.match() with RegExp /[^,]+/g to match one or more characters which are not comma ,

var ipd = 'field1,field2';
var ipdarray = ipd.match(/[^,]+/g);

Note that + operator converts array to string at

console.log('Img Pad array: ' + ipdarray); // field1,field2

use comma , operator at console.log() call

console.log('Img Pad array: ', ipdarray); // field1,field2
guest271314
  • 1
  • 15
  • 104
  • 177
0

var a = "field1,field2"

var b = a.split(',');

var c = b.map(function (v) {
return '"' + v + '"';
})

console.log(c);
console.log(c[0]);
console.log(c[1]);
Daniel Pérez
  • 1,873
  • 1
  • 17
  • 25