how to separate a string by comma in javascript? [duplicate] - javascript

This question already has answers here:
How do I split a string into an array of characters? [duplicate]
(8 answers)
How to get character array from a string?
(14 answers)
Closed 1 year ago.
how to print as follows in javascript ?
if my input is "smart"
then my output could be "s,m,a,r,t"
I've tried by using this logic but i got the output as
s,m,a,r,t,
let a = " ";
str = userInput[0];
console.log(str);
for (let i = 0; i < str.length; i++) {
a = a + str[i] + ",";
}
console.log(a.trim());

You can use the split method in js
var str = "smart";
var res = str.split("").join();
console.log(res)
//if you want it to remain an array don't do the join

Related

split string with '&' character and ignore if it's first in the string [duplicate]

This question already has answers here:
How can I match a pattern as long as it's not at the beginning with regex?
(6 answers)
Closed 2 years ago.
I would like to split string if & character is showing but it must ignore the split if & shows in the beginning of the string. example '&hello=world&hi=usa' would be
key: &hello, value: world
key: hi, value: usa
if i use split('&') it will create empty key and value because of the first &
You can simply remove the empty item from the array generated by the split() function:
let s = '&hello=world&hi=usa';
let a = s.split("&");
if (a[0] == "") {
a.shift(0);
}
let arr = [];
for (let i = 0; i < a.length; i++) {
let n = a[i].split("=");
let k = n[0];
let v = n[1];
arr.push({"key": k, "value": v});
}
console.log(arr);

Convert letters to lowercase from an array in JS [duplicate]

This question already has answers here:
Convert JavaScript String to be all lowercase
(15 answers)
Closed 3 years ago.
I have the following code:
var str = "abcabcABCABC"
var chars = str.split("");
var lettersCount = {};
for (var i = 0; i < chars.length;i++)
{
if (lettersCount[chars[i]] == undefined )
lettersCount[chars[i]] = 0;
lettersCount[chars[i]] ++;
}
for (var i in lettersCount)
{
console.log(i + ' = ' + lettersCount[i]);
}
This code is counting how many same letters are in a word. What am I trying is to convert the uppercase letters to lowercase so it should show like this: a - 4, b -4, now it shows: a - 2, A - 2.
I've just started with Js so please be good with me. :)
If you just need the string to be converted into lowercase letter then you can do it like this:-
var str = "abcabcABCABC";
var newStr = str.toLowerCase();
console.log(newStr);
Hope this helps.

get all possible combinations of some characters javascript [duplicate]

This question already has answers here:
Is there any pre-built method for finding all permutations of a given string in JavaScript?
(8 answers)
Closed 8 years ago.
So If a have A, B, C ,
I want to create some strings whith length 4
so the output will be
AAAA
AAAB
AAAC
AABA
AABB
AABC
ABAB
....
CCCC
There are some comments, so you can understand this. Thanks to http://www.walmik.com/2013/03/rearrange-letters-of-a-word/
function permutate(theWord){
//Array to store the generated words
var words = [];
/**
* Recursive function to split a string and rearrange
* it's characters and then join the results
*/
function rearrange(str, prefix) {
var i, singleChar, balanceStr, word;
//The first time round, prefix will be empty
prefix = prefix || '';
//Loop over the str to separate each single character
for(i = 0; i < str.length; i++) {
singleChar = str[i];
balanceStr = str.slice(0, i) + str.slice(i+1);
//join the prefix with each of the combinations
word = prefix + singleChar + balanceStr;
//Inject this word only if it does not exist
if(words.indexOf(word) < 0) words.push(word);
//Recursively call this function in case there are balance characters
if(balanceStr.length > 1) rearrange(balanceStr, prefix + singleChar);
}
}
//kick start recursion
rearrange(theWord);
return words;
}
var permutatedWord = permutate('goal');
console.log(permutatedWords);

Why this replace doesn't work Javascript? [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 7 years ago.
I want to know why this procedure doesn't replace words
I have to do a procedure which reads a string and replace all word like this {{employee.Name}} into a value on the ticket's scope
var mySplitResult = Val.split(' ');
for (var i = 0; i < mySplitResult.length; i++) {
if (mySplitResult[i].match("{{") && mySplitResult[i].match(".")) {
var start = mySplitResult[i].lastIndexOf(".") + 1;
var end = mySplitResult[i].indexOf("}}");
var result = mySplitResult[i].substring(start, end);
for (var key in ticket.PNData) {
if (key == result) {
change.replace(mySplitResult[i], ticket.PNData[key]);
alert(change)
}
}
}
}
In JavaScript strings are immutable which means you must assign the result to a variable.
mySplitResult[i] = mychange.replace(mySplitResult[i], ticket.PNData[key]);

Convert string of variables separated by & into array [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
I want to grab the variables contained in the url after somebody logs into my Facebook app.
For example:
// window.location = .../#access_token=CTTG4fT3ci...&expires_in=5298
hash = window.location.hash;
data = PARSE(hash);
console.log(data['access_token'] + ', ' + data['expires_in']);
// returns: CAAG4fT3ci..., 5298
Is there a method or function similar to JSON.parse() that would convert "hash" into an array or object?
function PARSE (hash) {
var l, chunk, i = 0, out = {};
var chunks = hash.substr(1).split('&');
for ( l = chunks.length; i < l; i++ ) {
chunk = chunks[i].split('=');
out[ chunk[0] ] = chunk[1];
}
return out;
}
Here's the fiddle: http://jsfiddle.net/VwLJS/

Categories