Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array to string containing comma's - JavaScript

Ok so I'm writing a script that includes whole sentences, but whole sentences can contain comma's.

Due to the way the script works the array has to be turned into a string at least once. So when that happens the comma's start conflicting with each other once I split the string back into the original values.

I can't quite figure out how to fix this and i've been looking all over with no success so far.

I'm working with chrome plugins, this is a small example of what happens:

var Data = ["This is a normal string", "This string, will cause a conflict.", "This string should be normal"];

// The data gets sent to a background script, in string form and comes back as this
var Data = "This is a normal string, This string, will cause a conflict., This string should be normal";
Data = Data.split(",");

// Results in 4 positions instead of 3
Data = ["This is a normal string", "This string"," will cause a conflict.", "This string should be normal"];
like image 961
Paradoxis Avatar asked Feb 17 '14 13:02

Paradoxis


People also ask

How do you convert an array element to a comma separated string?

The simplest way to convert an array to comma separated String is to create a StringBuilder, iterate through the array, and add each element of the array into StringBuilder after appending the comma.

How do you turn an array into a string in JavaScript?

Array.prototype.join() The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

How do you separate an array with a comma?

The comma separated list can be created by using implode() function. The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function.

How do you separate elements in an array?

Example-2: This example uses the splice() method to split the array into chunks of array. This method removes the items from the original array. This method can be used repeatedly to split array of any size. | Split array into chunks.


2 Answers

When you call .join() on the array in order to convert it to a string yo ucan specify the delimiter.

For example:

["Hello,","World"].join("%%%"); // "Hello,%%%World"

You can then split it based on "%%%" (.split("%%%")) in order to break it apart again.

That said, if you want to apply an action to each element of an array (every line) you probably do not have to call .join and then .split it again. Instead, you can use array methods, for example:

 var asLower = ["Hello","World"].map(function(line){ return line.toLowerCase(); });
 // as Lower now contains the items in lower case

Alternatively, if your goal is serialization instead of processing - you should not "roll your own" serialization and use the built in JSON.parse and JSON.stringify methods like h2oooooo suggested.

like image 67
Benjamin Gruenbaum Avatar answered Oct 13 '22 01:10

Benjamin Gruenbaum


You can use JSON.stringify(array) and JSON.parse(json) to make sure that whatever array/object you enter will come back the exact same (and it also works with booleans, integers, floats, etc.):

var data = ["This is a normal string", "This string, will cause a conflict.", "This string should be normal"];

// The data gets sent to a background script, in string form and comes back as this
data = JSON.stringify(data);
console.log(data);
// ["This is a normal string","This string, will cause a conflict.","This string should be normal"] 

data = JSON.parse(data);
console.log(data);
// ["This is a normal string", "This string, will cause a conflict.", "This string should be normal"] 
like image 38
h2ooooooo Avatar answered Oct 13 '22 01:10

h2ooooooo