Easy way to turn JavaScript array into comma-separated list?
The Array.prototype.join() method:
var arr = ["Zero", "One", "Two"];
document.write(arr.join(", "));
How to convert array into comma separated string in javascript
The method array.toString()
actually calls array.join()
which result in a string concatenated by commas. ref
var array = ['a','b','c','d','e','f'];document.write(array.toString()); // "a,b,c,d,e,f"
how to convert array of object to comma separated array of object
Why not! just turn it to JSON format, then remove open or closed square-brackets:
let arr = [{name:"k"},{name:"p"}];
console.log(JSON.stringify(arr).replace(/\[|\]/g,''))
Result as expected: {"name":"k"},{"name":"p"}
Convert array into comma separated strings
You could stringify the elements before joining.
var array = ["123", "456", "789"], result = array.map(a => JSON.stringify(a)).join(); console.log(result);
Easy way to turn JavaScript array into comma AND Quotation Marks separated string list?
You can use .map
to convert array elements to strings, and JSON.stringify
to get the resulting string:
const array = [1,2,3];
const result = JSON.stringify(array.map(String));
console.log(result);
Join nested array and output comma-separated list
You'll want to map
over your array to grab the text
prop out of it and then apply the desired join
.
const arr = [ {value: 1, text: 'one'}, {value: 2, text: 'two'}, {value: 3, text: 'three'}, {value: 4, text: 'four'}];
const output = arr.map(el => el.text).join(', ');
console.log(output);
get array, separate comma separated values and then recreate array
If you can ensure your nested arrays will always have two elements then it's quite simple:
const source = [
["Polymeric", "Vehicle Graphics (Basic)"],
["Cast", "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
["Polymeric", "Vehicle Graphics (Part Wrap)"]
];
// Flat map instructs that
// the nested arrays we're going to return
// should be spread onto the root one
source.flatMap(([key, value]) => { // [key, value] de-structs the array passed as a parameter
// Here we're getting an array of all possible values;
// if there's no ", " then we get a single element,
// otherwise we get the whole collection of values
const values = value.split(', ')
// Now, for each of the individual values we create the key-value pairs
return values.map(singleValue => [key, singleValue])
})
Yields:
Array destructuring JavaScript
Docs for flatMap
Array to Comma separated string and for last tag use the 'and' instead of comma in jquery
You can use .slice()
:
> var a = [1, 2, 3, 4, 5];
> [a.slice(0, -1).join(', '), a.slice(-1)[0]].join(a.length < 2 ? '' : ' and ');
'1, 2, 3, 4 and 5'
a.slice(0, -1).join(', ')
: takes all but the last element and joins them together with a comma.a.slice(-1)[0]
: it's the last element..join(a.length < 2 ? '' : ' and ')
: joins that string and the last element withand
if there are at least two elements.
How to convert object array to comma separated string?
You can use join
let arr = ["Varberg","Halmstad","Falkenberg"]
console.log(arr.join(','))
Turn properties of object into a comma-separated list?
You can use this one-liner in modern browsers
Object.keys(person).map(function(k){return person[k]}).join(",");
Related Topics
Push Method in React Hooks (Usestate)
Change Values in Array When Doing Foreach
Get Average Color of Image via JavaScript
How to Check If a Variable Is an Integer in JavaScript
Fastest Way to Convert JavaScript Nodelist to Array
Make React Useeffect Hook Not Run on Initial Render
Why How to Use a Function Before It's Defined in JavaScript
How to Implement Dynamic Getters/Setters in JavaScript
How to Make a JSONp Request from JavaScript Without Jquery
Why Does Typeof Array with Objects Return "Object" and Not "Array"
Get Mouse Wheel Events in Jquery
How to Chain Three Asynchronous Calls Using Jquery Promises
Iterate Through Nested JavaScript Objects
How to Download a File Using Window.Fetch
How to Replace Dom Element in Place Using JavaScript
How to Dodge Jquery Promises Completely When Chaining Two Async Jquery Functions