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.

2
  • How about console.log('Img Pad array: ', ipdarray)? Commented Jun 5, 2017 at 1:23
  • Array is an Object.... Commented Jun 5, 2017 at 1:39

3 Answers 3

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));

Sign up to request clarification or add additional context in comments.

4 Comments

but this is not an array, is a string
@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 - Thanks for the explanation, much appreciated
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"']
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

Comments

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.