This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
array join() method without a separator
I'm using .join() to convert my array to a string so I can output it in a text box as the user selects numbers in a calculator, I'm not entirely sure how I can remove the commas that are also being output in the list however. Can someone advise how this can be achieved or if there is a different approach I should be using?
JS
$(document).ready(function() {
var total = 0;
var arr = [];
//Testing
$('#calculator').children('.num').on('click', function(e) {
var clickedNumber = $(this).data('id');
arr.push(clickedNumber);
console.log(arr.join());
e.preventDefault();
});
});
JS Fiddle http://jsfiddle.net/CVr25/
Simply like that:
arr.join("")
You can specify an empty string as an argument to join, if no argument is specified a comma is used.
arr.join('');
http://jsfiddle.net/mowglisanu/CVr25/1/
The .join() method has a parameter for the separator string. If you want it to be empty instead of the default comma, use
arr.join("");
All you need to do is :
arr.join('');
FIDDLE
Related
This question already has answers here:
Find the characters in a string which are not duplicated
(29 answers)
Closed 1 year ago.
I found many question about counting characters used in a string and solutions
but how can I get all character used in string? Im new to JavaScript, im do not know to output this
for example
"English-Language" to "E,n,g,l,i,s,h,-,L,a,u,e" or "Javascript String" to "J,a,v,s,c,r,i,p,t, ,S,n,g"
Thank you..
Very simple with Set: (just cast it back to an array)
console.log([...new Set("English-Language")]);
console.log([...new Set("Javascript String")]);
And if you want it as a single string, do this:
console.log([...new Set("English-Language")].join(","));
console.log([...new Set("Javascript String")].join(","));
You can use the split function and the Set class to achieve this like so.
new Set("Javascript String".split(''))
The split('') part splits the string using an empty string as the seperator, so it just returns an array of all the characters. If you make a set from that array, it'll remove the duplicates. You can even pass the string directly to the Set constructor. If you want it as an array, just use Array.from.
you can simply use str.split("");
const str = 'English-Language';
str.split("");
Let me know if you face any future issue.
or Second option is
console.log([...new Set("English-Language")]);
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a string with a list of filenames such as
var string = '1.jpg,2.jpg,3.png,4.jpg,5.webp'
Is there a way to remove everything that doesn't end in .jpg so the output would look like this:
var newstring = '1.jpg,2.jpg,4.jpg'
You may write something like this
string
.split(",")
.filter(value => value.endsWith(".jpg"))
.join(",")
Did you experiment with possible regular expressions you could use? You might be able to find the answer yourself thanks to this page from the Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
If your string is always a comma separated list, then split the string on commas, which will give you an array of items. Then splice the array and remove items that contain the .jpg pattern.
var string = '1.jpg,2.jpg,3.png,4.jpg,5.webp';
string.split(',').filter((name)=> name.includes('.jpg')).join(',');
//"1.jpg,2.jpg,4.jpg"
var string = '1.jpg,2.jpg,3.png,4.jpg,5.webp';
var stringArray=string.split(',');
newArray=[];
stringArray.forEach(element => {
if(element.indexOf('.jpg')>-1){ newArray.push(element)}
});
console.log("jpg Array :"+newArray)// output : jpg Array :1.jpg,2.jpg,4.jpg
This question already has answers here:
Filter array by string length in javascript [duplicate]
(3 answers)
Closed 3 years ago.
I have a string for example:-
String: Notification 'Arcosa-Incident assigned to my group' 8ff7afc6db05eb80bfc706e2ca96191f included recipients as manager of a group in the notification's "Groups" field: 'Ruchika Jain' 9efa38ba0ff1310031a1e388b1050e3f
So basically i convert it into an array using .split(' ') method to make it comma separated values, now i want to filter this array and want only values which are 32 character long and remove rest of values.
Please help me achieve this. Alternate solutions are also welcomed. Thanks in advance.
Assuming you want to grab those IDs you can simply use a regex with match on the string without splitting/filtering it. (Note: I had to escape the single quotes in the text.)
const str = 'String: Notification \'Arcosa-Incident assigned to my group\' 8ff7afc6db05eb80bfc706e2ca96191f included recipients as manager of a group in the notification\'s "Groups" field: \'Ruchika Jain\' 9efa38ba0ff1310031a1e388b1050e3f';
const matches = str.match(/[a-f0-9]{32}/g);
console.log(matches);
Like so:
var arr = ...;
var filtered = arr.filter(word => word.length === 32);
Edit: this may be a bad idea if you want to parse only the GUIDs. It could certainly be that a name like "Ruchika" is also 32 characters long. Maybe, consider using regular expressions instead.
This question already has answers here:
Extract numbers from a string using javascript
(4 answers)
Closed 6 years ago.
I want to split the numbers out of a string and put them in an array using Regex.
For example, I have a string
23a43b3843c9293k234nm5g%>and using regex I need to get [23,43,3843,9293,234,5]
in an array
how can i achieve this?
Use String.prototype.match()
The match() method retrieves the matches when matching a string against a regular expression
Edit: As suggested by Tushar, Use Array.prototype.map and argument as Number to cast it as Number.
Try this:
var exp = /[0-9]+/g;
var input = "23a43b3843c9293k234nm5g%>";
var op = input.match(exp).map(Number);
console.log(op);
var text = "23a43b3843c9293k234nm5g%>";
var regex = /(\d+)/g;
alert(text.match(regex));
You get a match object with all of your numbers.
The script above correctly alerts 23,43,3843,9293,234,5.
see Fiddle http://jsfiddle.net/5WJ9v/307/
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I have a simple string that I'm trying to manipulate:
Your order will be processed soon:
I grab the string using:
var html = jQuery('.checkout td h4').html();
I then try to replace the ':' using:
html.replace(":", ".");
When I print it out to the console, the string is the same as the original string. I've also tried making sure that the html variable is of type "string" by doing the following:
html = html + "";
That doesn't do anything. In searching around, it seems that the replace function does a RegEx search and that the ":" character might have a special meaning. I do not know how to fix this. Can someone help me get rid of this stinkin' colon?
Slightly related...
I couldn't get these answers to work to replace all ":" in a string for the url encoded character %3a and modified this answer by'xdazz' to work: Javascript: Replace colon and comma characters to get...
str = str.replace(/:\s*/g, "%3a");
In your case it would be
str = str.replace(/:\s*/g, ".");
If you wanted to replace all colons with periods on a longer string.
Hope this helps somebody else.
The replace function returns a new string with the replacements made.
Javascript strings are immutable—it cannot modify the original string.
You need to write html = html.replace(":", ".");
I think c++ is the only high level language where strings are mutable. This means that replace cannot modify the string it operates on and so must return a new string instead.
Try the following instead
var element = jQuery('.checkout td h4');
element.html(element.html().replace(":", "."));
Or, perhaps more correctly (since you may have multiple elements).
jQuery('.checkout td h4').html(
function (index, oldHtml) {
return oldHtml.replace(":", ".");
}
);