Map, and Removing Commas from an Array in JavaScript

Map, and removing commas from an array in Javascript

My solution using join and map.

var ages = [23, 22, 4, 101, 14, 78, 90, 2, 1, 89, 19];

document.write(ages.map(a => Math.sqrt(a)).join("<br>"));

I think this solution is pretty concise and does just what you want.

How do I remove commas from an array of strings

You can use JavaScript Array#map with RegEx to remove extra commas.

arr.map(e => e.replace(/(,\s*)+/, ','));

ES5 Equivalent:

arr.map(function (e) {
return e.replace(/(,\s*)+/, ',');
});

RegEx Demo

The regex (,\s*)+ will search for one or more commas separated by any number of spaces between them.

var arr = ["1 Trenchard Road, , , , Saltford, Bristol, Avon", "10 Trenchard Road, , , , Saltford, Bristol, Avon", "11 Trenchard Road, , , , Saltford, Bristol, Avon", "12 Trenchard Road, , , , Saltford, Bristol, Avon"];
arr = arr.map(e => e.replace(/(,\s*)+/, ', '));console.log(arr);
document.getElementById('result').innerHTML = JSON.stringify(arr, 0, 4);
<pre id="result"></pre>

Javascript - Removing commas from an array of string numbers

You can use number in same map callback function, g to remove all occurrence of ,

dataArr = [" 1,431,417 ", " 1,838,127 ", " 679,974 ", " 2,720,560 ", " 544,368 ", " 1,540,370 "]
let arrData = dataArr.map(e => Number(e.replace(/(,\s*)+/g, '').trim()));console.log(arrData)

javascript map function to strip comma and keep unique values

You can use map() instead of filter and ES6 Set to return only unique elements.

var items = ['mum, dad', 'uncle, dad', 'brother, sister'];var clean = [...new Set([].concat(...items.map(e => e.split(', '))))]
console.log(clean)

How can I remove the commas from a html list created from an array of objects?

When you are creating your Array m and using the same m to be rendered in the page it automatically takes the comma as a part of the array.

Just join the array with white space before using it

document.getElementById('container').innerHTML = a + m.join('') + b;

Removing trailing comma while rendering elements in array.map

Your code works fine, so why bother to find another way to write it?

But perhaps, you don't need to insert the comma manually, just leave it to css:

const renderElements = () => {
if (!(data && data.length)) return [];
return data.map(({ id, name }) => (
<p key={id}>{name}</p>
));
}


return (
<div>
{renderElements()}
</div>
);

div p {
display: inline;
}

div p:not(:last-of-type)::after {
content: ',';
}
<div>
<p>Alice</p>
<p>Bob</p>
<p>Charlie</p>
<p>David</p>
</div>

Remove commas from arrays located inside an array

Edit to add additional explanation and reading:

  • https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/
  • https://dev.to/promhize/what-you-need-to-know-about-javascripts-implicit-coercion-e23
  • https://www.ecma-international.org/ecma-262/6.0/#sec-type-conversion

    (ecma's table is the most accurate and concise to read, but its presentation may be a bit spartan to read. I'll be honest I'm not satisfied with the bloggers' overly verbose explanations of implicit coercion either.)

Implicit coercion is triggered by the binary + operator, when any
operand is a string

You need to use an array and push to it. Using the + operator will coerce the array into a string and concatenate the strings.

var fruits = [["Bananas", "2"], ["Apples", "4"], ["Oranges", "5"]];var fruitsToday = [];  for (i=0; i < fruits.length; i++) {    fruitsToday.push(fruits[i].join(" - "));  }  document.getElementById("today").innerHTML = fruitsToday.join("<br>");
<div id="today"></div>

How can I remove commas or whatever from within a string?

You could add the commas to the list of characters to be replaced with empty strings:

function toString (convert) {
return convert.toString().replace(/[!.,]/g, '');
// Here ------------------------------^
}

EDIT:

To address the concern the the comments, you could map the array and apply the regex to each string in it individually, and only then call toString on it:

function toString (convert) {
return convert.map(s => s.replace(/[!.,]/g, '')).toString();
}

Commas appearing in html after array mapping

Use the .join('') method on array, to convert to a string without the commas

...  
h.insertAdjacentHTML("afterend", images.join(''));


Related Topics



Leave a reply



Submit