Simple:

var data = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}]
var result = data.map(function(val) {
  return val.location_id;
}).join(',');

console.log(result)

I assume you wanted a string, hence the .join(','), if you want an array simply remove that part.

Answer from Chrillewoodz on Stack Overflow
Discussions

Get comma separated string from array
Is it correct to assume you made a typo and what you want is one string instead of several comma-separated strings? ... why do you want to convert a JSON array (with is supported by the JSON format) to a comma separated string? I can't think of a good reason to do it. More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 24, 2017
How to get convert json array to comma separated values
Find answers to How to get convert json array to comma separated values from the expert community at Experts Exchange More on experts-exchange.com
๐ŸŒ experts-exchange.com
April 8, 2015
javascript - JSON Object into Comma Separated String
I have problem with converting JSON Object into string with comma separated, Here's my code: $.ajax({ url:' ', method:'get', success: More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to get a comma separated list from a JSON response?
Saying "he should actually have an array" is pretty useless because s/he doesn't; that's a different question. ... This is very simple in Javascript. You can access the variable data like this: ... which should alert "San Diego acls". Do the same for all of them using a loop. You can concatenate strings ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 16, 2012
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 28652216 โ€บ How-to-get-convert-json-array-to-comma-separated-values.html
Solved: How to get convert json array to comma separated values | Experts Exchange
April 8, 2015 - What exception do you get and what JSONTokener are you using ? http://www.massapi.com/class/js/JSONTokener-1.html#Example6 And I believe this should work ยท String input ='[{"filter":"ID","filterValues":["050886","050885","050884","050883","050882"]}]'; JSONArray jsonarray = (JSONArray) new JSONTokener(input ).nextValue(); for (int i = 0; i < jsonarray.length(); i++) { JSONObject jsonobject = jsonarray.getJSONObject(i); String field= jsonobject.getString("filter"); String values= jsonobject.getJSONObject("filterValues"); }
Top answer
1 of 3
3

This can be achieved via the join() method which is built into the Array type:

const object = {
"id" : "122223232244",
"title" : "ื”ืชืจืขืช ืคื™ืงื•ื“ ื”ืขื•ืจืฃ",
"data" : ["ืขื•ื˜ืฃ ืขื–ื” 218","ืขื•ื˜ืฃ ืขื–ื” 217"]
}

/* Join elements of data array in object to a comma separated string */
const value = object.data.join();

console.log(value);

If no separator argument is supplied, then the join() method will default to use a comma separator by default.

Update

If the JSON was supplied in raw text via a string you can use the JSON.parse() method to extract an object from the JSON string value as a first step like so:

const json = `{"id" : "122223232244","title" : "ื”ืชืจืขืช ืคื™ืงื•ื“ ื”ืขื•ืจืฃ","data" : ["ืขื•ื˜ืฃ ืขื–ื” 218","ืขื•ื˜ืฃ ืขื–ื” 217"]}`

/* Parse input JSON string */
const object = JSON.parse(json);

/* Join elements of data array in object to a comma separated string */
const value = object.data.join();

console.log(value);

2 of 3
3

Access object properties using dot notation (e.g. obj.data) and then on the array you can use join to convert to a string with a comma in between.

const obj = {
    "id" : "122223232244",
    "title" : "ื”ืชืจืขืช ืคื™ืงื•ื“ ื”ืขื•ืจืฃ",
    "data" : ["ืขื•ื˜ืฃ ืขื–ื” 218","ืขื•ื˜ืฃ ืขื–ื” 217"]
}

console.log(obj.data.join(', '))

Find elsewhere
๐ŸŒ
n8n
community.n8n.io โ€บ questions
Json to array comma - Questions - n8n Community
May 16, 2022 - Hello everyone. I trying to convert a json data to a string comma separated. Json is like this one: [ { "id": "625d757724acbe52634e6172", "idBoard": "625850ac4247c214ff3dc167", "name": "ANGELIS", "color": "yellow" }, { "id": "625d57da6bfa728b6684ed43", "idBoard": "625850ac4247c214ff3dc167", ...
๐ŸŒ
Reddit
reddit.com โ€บ r/javascripttips โ€บ how do i convert an array to a string with commas in javascript
r/JavaScriptTips on Reddit: How do I convert an array to a string with commas in JavaScript
April 13, 2023 -

In JavaScript, you can convert an array to a string with commas using the join()
method.

The join() method returns a string that concatenates all the elements of an array, separated by the specified separator, which in this case is a comma.

Here is an example:

const array = ['apple', 'banana', 'orange'];
const string = array.join(', ');
console.log(string); 

// output: "apple, banana, orange"

In this example, we first define an array of three fruits. Then we use the join()
method with a comma and a space as the separator to create a string that lists all the fruits with a comma and a space between each one.

You can replace the comma and space separator with any other separator you like, such as a hyphen, a semicolon, or a newline character.

It's important to note that the join() method only works on arrays, and it will throw an error if you try to use it on any other type of object.

Click here to learn more ways to Convert Array to String with Commas in JS

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ create-a-comma-separated-list-from-an-array-in-javascript
Create a Comma Separated List from an Array in JavaScript | GeeksforGeeks
December 28, 2024 - The code uses map(String) to convert each element of the array arr into a string, and then join(',') joins these string elements with commas. The result is a comma-separated string "1,2,3,4,5", which is logged to the console.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_join.asp
JavaScript Array join() Method
The join() method returns an array as a string. The join() method does not change the original array. Any separator can be specified. The default is comma (,).
๐ŸŒ
ServiceNow Community
servicenow.com โ€บ community โ€บ developer-forum โ€บ convert-array-string-to-comma-separated-string โ€บ m-p โ€บ 2606171
Convert Array String to Comma Separated String - ServiceNow Community
July 6, 2023 - Hi, I suspect your issue is that your array is an array of objects, and so may need to iterate through the sorted array stringifying each element\object and concatenating these individually to your output string. ... var tempString = ''; for(key in sortedArray) { tempString = tempString + JSON.stringify(sortedArray[key]); }