How to convert an array like string to array in node.js?

Actually I'm getting the arraylist from android device in node.js . But as it's in string form so I wanna convert it into an array . For that I've referred a lot of similar questions in SO but none of them were helpful . I also tried to use JSON.parse() but it was not helpful.

I'm getting societyList in form '[Art, Photography, Writing]'.Thus how to convert this format to an array?

Code:

var soc_arr=JSON.parse(data.societyList)
            console.log(soc_arr.length)

use something like this

var array = arrayList.replace(/^\[|\]$/g, "").split(", ");
UPDATE:

After @drinchev suggestion regex used.

regex matches char starts with '[' and ends with ']'

How to convert an array-like string to an array in Node.js?, Use JSON.parse to parse string array to JS array. var timingsAr = '[["11:00 AM", "1​:00 PM"], ["1:00 PM", "4:00 PM"]]' JSON.parse(timingsAr);  Converting a Byte Array to String with Buffer in Node.JS If you're working with Node.JS, you may have encountered Buffer s which are a way to store binary data as a sequence of its bytes. But what happens when you have serialised it, and you want to convert it back to a String?

This string is not valid JSON since it does not use the "" to indicate a string.

The best way would be to parse it yourself using a method like below:

let data = '[test1, test2, test3]';
let parts = data
  .trim() // trim the initial data!
  .substr(1,data.length-2) // remove the brackets from string
  .split(',') // plit the string using the seperator ','
  .map(e=>e.trim()) // trim the results to remove spaces at start and end
  
console.log(parts);

Array.from(), JavaScript's string split method returns an array of substrings obtained by Pass the separator you want to use to split the string as the first argument, and a new  You can also use Function.prototype.call to call Array.prototype methods on Array-like objects directly, without converting them: 5.1 var domList = document.querySelectorAll('#myDropdown option'); domList.forEach(function() { // Do stuff }); // Error! forEach is not defined.

RegExp.match() maybe

 console.log('[Art, Photography, Writing]'.match(/\w+/g))

Array.prototype.join(), Learn how to convert an array to a string using JavaScript using the join You can hack along in either your web browser's console or using the Node.js REPL. being joined together as a string, using the comma , character as the delimiter. Let’s take a various example for convert string to array JavaScript with different technics. How to Convert Comma Separated String into an Array in JavaScript? Let’s take a new example for convert comma-separated string into an array in JavaScript. Insert the splitting method on the string method you want to split into array elements.

Use replace and split. In addition, use trim() to remove the trailing and leading whitespaces from the array element.

var str = '[Art, Photography, Writing]';
var JSONData = str.replace('[','').replace(']','').split(',').map(x => x.trim());
console.log(JSONData);

Convert String to Array with JavaScript's String split Method, We take a look at how to convert array-like objects such as NodeList into include the special variable arguments, DOM node lists, and strings. What if you have a nested array? That is, an array with an array inside it? Here you need to use JSON.stringify(). Finally, if you can’t use a built-in method for some reason, you can loop through the array and convert it to a string manually.

Converting Arrays to Strings in JavaScript ← Alligator.io, There are a number of useful ECMAScript 5 features which are supported by V8, such as Array.forEach(), Array.indexOf(), Object.keys() and String.trim(). The output like: Priya,13. String to Array in Javascript. Next we move to how to convert the array value to string and then it convert to array and finally the data is displayed in dropdown list. Are you confused?. Everybody think as normal why we convert array to string then to array, easily we can convert string array value into dropdown list.

JS Array From an Array-Like Object, How to convert an Object {} to an Array [] of key-value pairs in JavaScript? How to pass an array as a function parameter in JavaScript ? Index inside map()  Byte Array: [0, 10, 20, 30, 40] Hex String Conversion: 000A141E28 append(.format) method for conversion of byte array into hex string in Java. Another method that can be used is to include append(.format) using stringBuilder in Java. It works by converting every single value from byte array and convert one by one to hex string accordingly

5. Arrays, Objects, Functions and JSON, How to convert an Object {} to an Array [] of key-value pairs in JavaScript? How to pass an array as a function parameter in JavaScript ? Difference between node​.js require and ES6 import and export · Introduction to JavaScript · How to  In this video we will see how to convert array to string. By using jvdroid. Thank you. Please follow. Skip navigation Sign in. Like this video? Sign in to make your opinion count. Sign in. 1 0.

Comments
  • Define societyList like '["Art", "Photography", "Writing"]' then JSON.parse() should work fine
  • @Satpal sir that's the problem how to do that dynamically
  • Even better list.replace(/^\[|\]$/, "").split( ", " ); :)
  • @drinchev oups thats not correct can you verify your regex ? im getting a bracket at the last string like ["Art", "Photography", "Writing]"]
  • Oops sorry list.replace(/^\[|\]$/g, "").split( ", " )
  • This will break for ["Hello, World", "Hi"]
  • var array = arrayList.replace(/^\["|"\]$/g, "").split('", "'); should fix it (for valid JSON), but as someone pointed out the example string is not valid json
  • except in this case you assume each array element is a single word made entirely of ASCII letters. That may be a stretch.
  • @Touffy not really much to go in in the question
  • I agree. Which is why you shouldn't make assumptions.