Is there any way yo make a java script string into an array then change the array values then lastly make it back into a string
I want to have a user inputted string like "hello world". then take the string and turn it into an array with each letter being a part of the array. then i want to change to the values of the array. then i want to take the array and combine all the parts of the array into a string then output it. so the out put would look like "gwkki qieks" note the replacement values are coming from another array.
javascript arrays string
add a comment |
I want to have a user inputted string like "hello world". then take the string and turn it into an array with each letter being a part of the array. then i want to change to the values of the array. then i want to take the array and combine all the parts of the array into a string then output it. so the out put would look like "gwkki qieks" note the replacement values are coming from another array.
javascript arrays string
2
Indeed. You can usevar arr = str.split("")
, which you can then make your changes, then to convert it into an array usearr.join("")
. There is a probably a similar question already asked before, I'll see if I can find it.
– Spencer Wieczorek
Nov 14 '18 at 23:09
Welcome to StackOverflow! It's required when you ask that you've attempted to resolve your query and posted the results of that, that you've done a bit of research, and that you clearly let us know what the result is that you're looking for. Please see stackoverflow.com/help/how-to-ask
– zfrisch
Nov 14 '18 at 23:12
Related: stackoverflow.com/questions/16576983/…
– Spencer Wieczorek
Nov 14 '18 at 23:16
add a comment |
I want to have a user inputted string like "hello world". then take the string and turn it into an array with each letter being a part of the array. then i want to change to the values of the array. then i want to take the array and combine all the parts of the array into a string then output it. so the out put would look like "gwkki qieks" note the replacement values are coming from another array.
javascript arrays string
I want to have a user inputted string like "hello world". then take the string and turn it into an array with each letter being a part of the array. then i want to change to the values of the array. then i want to take the array and combine all the parts of the array into a string then output it. so the out put would look like "gwkki qieks" note the replacement values are coming from another array.
javascript arrays string
javascript arrays string
asked Nov 14 '18 at 23:04
useruser
1
1
2
Indeed. You can usevar arr = str.split("")
, which you can then make your changes, then to convert it into an array usearr.join("")
. There is a probably a similar question already asked before, I'll see if I can find it.
– Spencer Wieczorek
Nov 14 '18 at 23:09
Welcome to StackOverflow! It's required when you ask that you've attempted to resolve your query and posted the results of that, that you've done a bit of research, and that you clearly let us know what the result is that you're looking for. Please see stackoverflow.com/help/how-to-ask
– zfrisch
Nov 14 '18 at 23:12
Related: stackoverflow.com/questions/16576983/…
– Spencer Wieczorek
Nov 14 '18 at 23:16
add a comment |
2
Indeed. You can usevar arr = str.split("")
, which you can then make your changes, then to convert it into an array usearr.join("")
. There is a probably a similar question already asked before, I'll see if I can find it.
– Spencer Wieczorek
Nov 14 '18 at 23:09
Welcome to StackOverflow! It's required when you ask that you've attempted to resolve your query and posted the results of that, that you've done a bit of research, and that you clearly let us know what the result is that you're looking for. Please see stackoverflow.com/help/how-to-ask
– zfrisch
Nov 14 '18 at 23:12
Related: stackoverflow.com/questions/16576983/…
– Spencer Wieczorek
Nov 14 '18 at 23:16
2
2
Indeed. You can use
var arr = str.split("")
, which you can then make your changes, then to convert it into an array use arr.join("")
. There is a probably a similar question already asked before, I'll see if I can find it.– Spencer Wieczorek
Nov 14 '18 at 23:09
Indeed. You can use
var arr = str.split("")
, which you can then make your changes, then to convert it into an array use arr.join("")
. There is a probably a similar question already asked before, I'll see if I can find it.– Spencer Wieczorek
Nov 14 '18 at 23:09
Welcome to StackOverflow! It's required when you ask that you've attempted to resolve your query and posted the results of that, that you've done a bit of research, and that you clearly let us know what the result is that you're looking for. Please see stackoverflow.com/help/how-to-ask
– zfrisch
Nov 14 '18 at 23:12
Welcome to StackOverflow! It's required when you ask that you've attempted to resolve your query and posted the results of that, that you've done a bit of research, and that you clearly let us know what the result is that you're looking for. Please see stackoverflow.com/help/how-to-ask
– zfrisch
Nov 14 '18 at 23:12
Related: stackoverflow.com/questions/16576983/…
– Spencer Wieczorek
Nov 14 '18 at 23:16
Related: stackoverflow.com/questions/16576983/…
– Spencer Wieczorek
Nov 14 '18 at 23:16
add a comment |
3 Answers
3
active
oldest
votes
As mentioned in my comment you can use .split("")
to convert your string into an array of characters. You can then use .join("")
to convert that back into a string after making any modifications.
For this method I'd recommend using a object as it will act like a hash map, that way you don't need to iterate though all your replacement characters each time:
var str = "hello world"; // Original String
var map = // Used for char replacement
'h': 'g',
'e': 'w',
'l': 'k',
'o': 'i',
'w': 'q',
'r': 'e',
'd': 's'
;
var arr = str.split(""); // Convert to array of chars
for(var i=0; i<arr.length; i++) // For each char in our array
if(arr[i] in map) // If the char in our map
arr[i] = map[arr[i]]; // Then replace it
console.log(arr.join("")); // Join combines all chars to string
Edit:
As @noahnu pointed out you can do this using map
. Instead of a standard for loop you can use:
str.split("").map(c => map[c] || c).join("");
Or in ES5:
str.split("").map(function(c) c).join(""));
This will make the current character being iterated to be the value map[c]
, if it is undefined
that will evaluate to false
and it will then use c
instead.
1
...or a one liner which is still pretty readable:'hello world'.split('').map(c => map[c] || c).join('')
– noahnu
Nov 15 '18 at 0:03
1
@noahnu Yes this can be done in less code. Since OP appears to be a beginner/student I wanted to make this more pseudocode-like so they can more clearly understand how to logically solve their problem.
– Spencer Wieczorek
Nov 15 '18 at 0:17
yes I figured, that's why I kept it as a comment to show the alternative
– noahnu
Nov 15 '18 at 0:25
add a comment |
Looks like you're doing this – https://www.mehtanirav.com/qwerty-werty/
The letters in a string are mapped to the corresponding keyboard keys and shifted so that Q becomes P, W becomes Q and so on. By storing all of the shifts in a dictionary, we can iterate through the characters of the string and append the corresponding mapped letter to the new string.
const keyboard =
q:'p',w:'q',e:'w',r:'e',t:'r',y:'t',u:'y',i:'u',o:'i',p:'o',
a:'l',s:'a',d:'s',f:'d',g:'f',h:'g',j:'h',k:'j',l:'k',
z:'m',x:'z',c:'x',v:'c',b:'v',n:'b',m:'n',
Q:'P',W:'Q',E:'W',R:'E',T:'R',Y:'T',U:'Y',I:'U',O:'I',P:'O',
A:'L',S:'A',D:'S',F:'D',G:'F',H:'G',J:'H',K:'J',L:'K',
Z:'M',X:'Z',C:'X',V:'C',B:'V',N:'B',M:'N'
function qwertyWerty(str)
let newStr = '';
for(const char of str)
newStr += keyboard[char]
return newStr;
console.log(qwertyWerty("hello world"));
console.log(qwertyWerty("Hello World"));
add a comment |
It's pretty simple to do this.
- Create a mapping of your letters to your intended keys. For example,
h
tog
,e
tow
and so on. - Now use the
.split
to create an array of characters form the string. - Now use the
ARRAY.map(KEY => VALUE)
to create a new array with the new new values of the corresponding characters. - Now use the
ARRAY.join("")
to convert the list to a string.
Here is the code snippet. Hope this helps.
var string = "hello world"; // initial String
var dictionary = 'h': 'g', 'e': 'w', 'l': 'k', 'o': 'i', 'w': 'q', 'r': 'e', 'd': 's';
var old_array = string.split(""); // you'll get an array of characters
var new_array = old_array.map(c => dictionary[c] || c);
var new_string = new_array.join("");
console.log(new_string); // Join combines all chars to string
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310055%2fis-there-any-way-yo-make-a-java-script-string-into-an-array-then-change-the-arra%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
As mentioned in my comment you can use .split("")
to convert your string into an array of characters. You can then use .join("")
to convert that back into a string after making any modifications.
For this method I'd recommend using a object as it will act like a hash map, that way you don't need to iterate though all your replacement characters each time:
var str = "hello world"; // Original String
var map = // Used for char replacement
'h': 'g',
'e': 'w',
'l': 'k',
'o': 'i',
'w': 'q',
'r': 'e',
'd': 's'
;
var arr = str.split(""); // Convert to array of chars
for(var i=0; i<arr.length; i++) // For each char in our array
if(arr[i] in map) // If the char in our map
arr[i] = map[arr[i]]; // Then replace it
console.log(arr.join("")); // Join combines all chars to string
Edit:
As @noahnu pointed out you can do this using map
. Instead of a standard for loop you can use:
str.split("").map(c => map[c] || c).join("");
Or in ES5:
str.split("").map(function(c) c).join(""));
This will make the current character being iterated to be the value map[c]
, if it is undefined
that will evaluate to false
and it will then use c
instead.
1
...or a one liner which is still pretty readable:'hello world'.split('').map(c => map[c] || c).join('')
– noahnu
Nov 15 '18 at 0:03
1
@noahnu Yes this can be done in less code. Since OP appears to be a beginner/student I wanted to make this more pseudocode-like so they can more clearly understand how to logically solve their problem.
– Spencer Wieczorek
Nov 15 '18 at 0:17
yes I figured, that's why I kept it as a comment to show the alternative
– noahnu
Nov 15 '18 at 0:25
add a comment |
As mentioned in my comment you can use .split("")
to convert your string into an array of characters. You can then use .join("")
to convert that back into a string after making any modifications.
For this method I'd recommend using a object as it will act like a hash map, that way you don't need to iterate though all your replacement characters each time:
var str = "hello world"; // Original String
var map = // Used for char replacement
'h': 'g',
'e': 'w',
'l': 'k',
'o': 'i',
'w': 'q',
'r': 'e',
'd': 's'
;
var arr = str.split(""); // Convert to array of chars
for(var i=0; i<arr.length; i++) // For each char in our array
if(arr[i] in map) // If the char in our map
arr[i] = map[arr[i]]; // Then replace it
console.log(arr.join("")); // Join combines all chars to string
Edit:
As @noahnu pointed out you can do this using map
. Instead of a standard for loop you can use:
str.split("").map(c => map[c] || c).join("");
Or in ES5:
str.split("").map(function(c) c).join(""));
This will make the current character being iterated to be the value map[c]
, if it is undefined
that will evaluate to false
and it will then use c
instead.
1
...or a one liner which is still pretty readable:'hello world'.split('').map(c => map[c] || c).join('')
– noahnu
Nov 15 '18 at 0:03
1
@noahnu Yes this can be done in less code. Since OP appears to be a beginner/student I wanted to make this more pseudocode-like so they can more clearly understand how to logically solve their problem.
– Spencer Wieczorek
Nov 15 '18 at 0:17
yes I figured, that's why I kept it as a comment to show the alternative
– noahnu
Nov 15 '18 at 0:25
add a comment |
As mentioned in my comment you can use .split("")
to convert your string into an array of characters. You can then use .join("")
to convert that back into a string after making any modifications.
For this method I'd recommend using a object as it will act like a hash map, that way you don't need to iterate though all your replacement characters each time:
var str = "hello world"; // Original String
var map = // Used for char replacement
'h': 'g',
'e': 'w',
'l': 'k',
'o': 'i',
'w': 'q',
'r': 'e',
'd': 's'
;
var arr = str.split(""); // Convert to array of chars
for(var i=0; i<arr.length; i++) // For each char in our array
if(arr[i] in map) // If the char in our map
arr[i] = map[arr[i]]; // Then replace it
console.log(arr.join("")); // Join combines all chars to string
Edit:
As @noahnu pointed out you can do this using map
. Instead of a standard for loop you can use:
str.split("").map(c => map[c] || c).join("");
Or in ES5:
str.split("").map(function(c) c).join(""));
This will make the current character being iterated to be the value map[c]
, if it is undefined
that will evaluate to false
and it will then use c
instead.
As mentioned in my comment you can use .split("")
to convert your string into an array of characters. You can then use .join("")
to convert that back into a string after making any modifications.
For this method I'd recommend using a object as it will act like a hash map, that way you don't need to iterate though all your replacement characters each time:
var str = "hello world"; // Original String
var map = // Used for char replacement
'h': 'g',
'e': 'w',
'l': 'k',
'o': 'i',
'w': 'q',
'r': 'e',
'd': 's'
;
var arr = str.split(""); // Convert to array of chars
for(var i=0; i<arr.length; i++) // For each char in our array
if(arr[i] in map) // If the char in our map
arr[i] = map[arr[i]]; // Then replace it
console.log(arr.join("")); // Join combines all chars to string
Edit:
As @noahnu pointed out you can do this using map
. Instead of a standard for loop you can use:
str.split("").map(c => map[c] || c).join("");
Or in ES5:
str.split("").map(function(c) c).join(""));
This will make the current character being iterated to be the value map[c]
, if it is undefined
that will evaluate to false
and it will then use c
instead.
var str = "hello world"; // Original String
var map = // Used for char replacement
'h': 'g',
'e': 'w',
'l': 'k',
'o': 'i',
'w': 'q',
'r': 'e',
'd': 's'
;
var arr = str.split(""); // Convert to array of chars
for(var i=0; i<arr.length; i++) // For each char in our array
if(arr[i] in map) // If the char in our map
arr[i] = map[arr[i]]; // Then replace it
console.log(arr.join("")); // Join combines all chars to string
var str = "hello world"; // Original String
var map = // Used for char replacement
'h': 'g',
'e': 'w',
'l': 'k',
'o': 'i',
'w': 'q',
'r': 'e',
'd': 's'
;
var arr = str.split(""); // Convert to array of chars
for(var i=0; i<arr.length; i++) // For each char in our array
if(arr[i] in map) // If the char in our map
arr[i] = map[arr[i]]; // Then replace it
console.log(arr.join("")); // Join combines all chars to string
edited Nov 15 '18 at 0:25
answered Nov 14 '18 at 23:27
Spencer WieczorekSpencer Wieczorek
17.6k43345
17.6k43345
1
...or a one liner which is still pretty readable:'hello world'.split('').map(c => map[c] || c).join('')
– noahnu
Nov 15 '18 at 0:03
1
@noahnu Yes this can be done in less code. Since OP appears to be a beginner/student I wanted to make this more pseudocode-like so they can more clearly understand how to logically solve their problem.
– Spencer Wieczorek
Nov 15 '18 at 0:17
yes I figured, that's why I kept it as a comment to show the alternative
– noahnu
Nov 15 '18 at 0:25
add a comment |
1
...or a one liner which is still pretty readable:'hello world'.split('').map(c => map[c] || c).join('')
– noahnu
Nov 15 '18 at 0:03
1
@noahnu Yes this can be done in less code. Since OP appears to be a beginner/student I wanted to make this more pseudocode-like so they can more clearly understand how to logically solve their problem.
– Spencer Wieczorek
Nov 15 '18 at 0:17
yes I figured, that's why I kept it as a comment to show the alternative
– noahnu
Nov 15 '18 at 0:25
1
1
...or a one liner which is still pretty readable:
'hello world'.split('').map(c => map[c] || c).join('')
– noahnu
Nov 15 '18 at 0:03
...or a one liner which is still pretty readable:
'hello world'.split('').map(c => map[c] || c).join('')
– noahnu
Nov 15 '18 at 0:03
1
1
@noahnu Yes this can be done in less code. Since OP appears to be a beginner/student I wanted to make this more pseudocode-like so they can more clearly understand how to logically solve their problem.
– Spencer Wieczorek
Nov 15 '18 at 0:17
@noahnu Yes this can be done in less code. Since OP appears to be a beginner/student I wanted to make this more pseudocode-like so they can more clearly understand how to logically solve their problem.
– Spencer Wieczorek
Nov 15 '18 at 0:17
yes I figured, that's why I kept it as a comment to show the alternative
– noahnu
Nov 15 '18 at 0:25
yes I figured, that's why I kept it as a comment to show the alternative
– noahnu
Nov 15 '18 at 0:25
add a comment |
Looks like you're doing this – https://www.mehtanirav.com/qwerty-werty/
The letters in a string are mapped to the corresponding keyboard keys and shifted so that Q becomes P, W becomes Q and so on. By storing all of the shifts in a dictionary, we can iterate through the characters of the string and append the corresponding mapped letter to the new string.
const keyboard =
q:'p',w:'q',e:'w',r:'e',t:'r',y:'t',u:'y',i:'u',o:'i',p:'o',
a:'l',s:'a',d:'s',f:'d',g:'f',h:'g',j:'h',k:'j',l:'k',
z:'m',x:'z',c:'x',v:'c',b:'v',n:'b',m:'n',
Q:'P',W:'Q',E:'W',R:'E',T:'R',Y:'T',U:'Y',I:'U',O:'I',P:'O',
A:'L',S:'A',D:'S',F:'D',G:'F',H:'G',J:'H',K:'J',L:'K',
Z:'M',X:'Z',C:'X',V:'C',B:'V',N:'B',M:'N'
function qwertyWerty(str)
let newStr = '';
for(const char of str)
newStr += keyboard[char]
return newStr;
console.log(qwertyWerty("hello world"));
console.log(qwertyWerty("Hello World"));
add a comment |
Looks like you're doing this – https://www.mehtanirav.com/qwerty-werty/
The letters in a string are mapped to the corresponding keyboard keys and shifted so that Q becomes P, W becomes Q and so on. By storing all of the shifts in a dictionary, we can iterate through the characters of the string and append the corresponding mapped letter to the new string.
const keyboard =
q:'p',w:'q',e:'w',r:'e',t:'r',y:'t',u:'y',i:'u',o:'i',p:'o',
a:'l',s:'a',d:'s',f:'d',g:'f',h:'g',j:'h',k:'j',l:'k',
z:'m',x:'z',c:'x',v:'c',b:'v',n:'b',m:'n',
Q:'P',W:'Q',E:'W',R:'E',T:'R',Y:'T',U:'Y',I:'U',O:'I',P:'O',
A:'L',S:'A',D:'S',F:'D',G:'F',H:'G',J:'H',K:'J',L:'K',
Z:'M',X:'Z',C:'X',V:'C',B:'V',N:'B',M:'N'
function qwertyWerty(str)
let newStr = '';
for(const char of str)
newStr += keyboard[char]
return newStr;
console.log(qwertyWerty("hello world"));
console.log(qwertyWerty("Hello World"));
add a comment |
Looks like you're doing this – https://www.mehtanirav.com/qwerty-werty/
The letters in a string are mapped to the corresponding keyboard keys and shifted so that Q becomes P, W becomes Q and so on. By storing all of the shifts in a dictionary, we can iterate through the characters of the string and append the corresponding mapped letter to the new string.
const keyboard =
q:'p',w:'q',e:'w',r:'e',t:'r',y:'t',u:'y',i:'u',o:'i',p:'o',
a:'l',s:'a',d:'s',f:'d',g:'f',h:'g',j:'h',k:'j',l:'k',
z:'m',x:'z',c:'x',v:'c',b:'v',n:'b',m:'n',
Q:'P',W:'Q',E:'W',R:'E',T:'R',Y:'T',U:'Y',I:'U',O:'I',P:'O',
A:'L',S:'A',D:'S',F:'D',G:'F',H:'G',J:'H',K:'J',L:'K',
Z:'M',X:'Z',C:'X',V:'C',B:'V',N:'B',M:'N'
function qwertyWerty(str)
let newStr = '';
for(const char of str)
newStr += keyboard[char]
return newStr;
console.log(qwertyWerty("hello world"));
console.log(qwertyWerty("Hello World"));
Looks like you're doing this – https://www.mehtanirav.com/qwerty-werty/
The letters in a string are mapped to the corresponding keyboard keys and shifted so that Q becomes P, W becomes Q and so on. By storing all of the shifts in a dictionary, we can iterate through the characters of the string and append the corresponding mapped letter to the new string.
const keyboard =
q:'p',w:'q',e:'w',r:'e',t:'r',y:'t',u:'y',i:'u',o:'i',p:'o',
a:'l',s:'a',d:'s',f:'d',g:'f',h:'g',j:'h',k:'j',l:'k',
z:'m',x:'z',c:'x',v:'c',b:'v',n:'b',m:'n',
Q:'P',W:'Q',E:'W',R:'E',T:'R',Y:'T',U:'Y',I:'U',O:'I',P:'O',
A:'L',S:'A',D:'S',F:'D',G:'F',H:'G',J:'H',K:'J',L:'K',
Z:'M',X:'Z',C:'X',V:'C',B:'V',N:'B',M:'N'
function qwertyWerty(str)
let newStr = '';
for(const char of str)
newStr += keyboard[char]
return newStr;
console.log(qwertyWerty("hello world"));
console.log(qwertyWerty("Hello World"));
const keyboard =
q:'p',w:'q',e:'w',r:'e',t:'r',y:'t',u:'y',i:'u',o:'i',p:'o',
a:'l',s:'a',d:'s',f:'d',g:'f',h:'g',j:'h',k:'j',l:'k',
z:'m',x:'z',c:'x',v:'c',b:'v',n:'b',m:'n',
Q:'P',W:'Q',E:'W',R:'E',T:'R',Y:'T',U:'Y',I:'U',O:'I',P:'O',
A:'L',S:'A',D:'S',F:'D',G:'F',H:'G',J:'H',K:'J',L:'K',
Z:'M',X:'Z',C:'X',V:'C',B:'V',N:'B',M:'N'
function qwertyWerty(str)
let newStr = '';
for(const char of str)
newStr += keyboard[char]
return newStr;
console.log(qwertyWerty("hello world"));
console.log(qwertyWerty("Hello World"));
const keyboard =
q:'p',w:'q',e:'w',r:'e',t:'r',y:'t',u:'y',i:'u',o:'i',p:'o',
a:'l',s:'a',d:'s',f:'d',g:'f',h:'g',j:'h',k:'j',l:'k',
z:'m',x:'z',c:'x',v:'c',b:'v',n:'b',m:'n',
Q:'P',W:'Q',E:'W',R:'E',T:'R',Y:'T',U:'Y',I:'U',O:'I',P:'O',
A:'L',S:'A',D:'S',F:'D',G:'F',H:'G',J:'H',K:'J',L:'K',
Z:'M',X:'Z',C:'X',V:'C',B:'V',N:'B',M:'N'
function qwertyWerty(str)
let newStr = '';
for(const char of str)
newStr += keyboard[char]
return newStr;
console.log(qwertyWerty("hello world"));
console.log(qwertyWerty("Hello World"));
edited Nov 15 '18 at 0:39
answered Nov 14 '18 at 23:52
CallamCallam
8,67121824
8,67121824
add a comment |
add a comment |
It's pretty simple to do this.
- Create a mapping of your letters to your intended keys. For example,
h
tog
,e
tow
and so on. - Now use the
.split
to create an array of characters form the string. - Now use the
ARRAY.map(KEY => VALUE)
to create a new array with the new new values of the corresponding characters. - Now use the
ARRAY.join("")
to convert the list to a string.
Here is the code snippet. Hope this helps.
var string = "hello world"; // initial String
var dictionary = 'h': 'g', 'e': 'w', 'l': 'k', 'o': 'i', 'w': 'q', 'r': 'e', 'd': 's';
var old_array = string.split(""); // you'll get an array of characters
var new_array = old_array.map(c => dictionary[c] || c);
var new_string = new_array.join("");
console.log(new_string); // Join combines all chars to string
add a comment |
It's pretty simple to do this.
- Create a mapping of your letters to your intended keys. For example,
h
tog
,e
tow
and so on. - Now use the
.split
to create an array of characters form the string. - Now use the
ARRAY.map(KEY => VALUE)
to create a new array with the new new values of the corresponding characters. - Now use the
ARRAY.join("")
to convert the list to a string.
Here is the code snippet. Hope this helps.
var string = "hello world"; // initial String
var dictionary = 'h': 'g', 'e': 'w', 'l': 'k', 'o': 'i', 'w': 'q', 'r': 'e', 'd': 's';
var old_array = string.split(""); // you'll get an array of characters
var new_array = old_array.map(c => dictionary[c] || c);
var new_string = new_array.join("");
console.log(new_string); // Join combines all chars to string
add a comment |
It's pretty simple to do this.
- Create a mapping of your letters to your intended keys. For example,
h
tog
,e
tow
and so on. - Now use the
.split
to create an array of characters form the string. - Now use the
ARRAY.map(KEY => VALUE)
to create a new array with the new new values of the corresponding characters. - Now use the
ARRAY.join("")
to convert the list to a string.
Here is the code snippet. Hope this helps.
var string = "hello world"; // initial String
var dictionary = 'h': 'g', 'e': 'w', 'l': 'k', 'o': 'i', 'w': 'q', 'r': 'e', 'd': 's';
var old_array = string.split(""); // you'll get an array of characters
var new_array = old_array.map(c => dictionary[c] || c);
var new_string = new_array.join("");
console.log(new_string); // Join combines all chars to string
It's pretty simple to do this.
- Create a mapping of your letters to your intended keys. For example,
h
tog
,e
tow
and so on. - Now use the
.split
to create an array of characters form the string. - Now use the
ARRAY.map(KEY => VALUE)
to create a new array with the new new values of the corresponding characters. - Now use the
ARRAY.join("")
to convert the list to a string.
Here is the code snippet. Hope this helps.
var string = "hello world"; // initial String
var dictionary = 'h': 'g', 'e': 'w', 'l': 'k', 'o': 'i', 'w': 'q', 'r': 'e', 'd': 's';
var old_array = string.split(""); // you'll get an array of characters
var new_array = old_array.map(c => dictionary[c] || c);
var new_string = new_array.join("");
console.log(new_string); // Join combines all chars to string
var string = "hello world"; // initial String
var dictionary = 'h': 'g', 'e': 'w', 'l': 'k', 'o': 'i', 'w': 'q', 'r': 'e', 'd': 's';
var old_array = string.split(""); // you'll get an array of characters
var new_array = old_array.map(c => dictionary[c] || c);
var new_string = new_array.join("");
console.log(new_string); // Join combines all chars to string
var string = "hello world"; // initial String
var dictionary = 'h': 'g', 'e': 'w', 'l': 'k', 'o': 'i', 'w': 'q', 'r': 'e', 'd': 's';
var old_array = string.split(""); // you'll get an array of characters
var new_array = old_array.map(c => dictionary[c] || c);
var new_string = new_array.join("");
console.log(new_string); // Join combines all chars to string
answered Nov 15 '18 at 5:44
RaiRai
1,0263822
1,0263822
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310055%2fis-there-any-way-yo-make-a-java-script-string-into-an-array-then-change-the-arra%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Indeed. You can use
var arr = str.split("")
, which you can then make your changes, then to convert it into an array usearr.join("")
. There is a probably a similar question already asked before, I'll see if I can find it.– Spencer Wieczorek
Nov 14 '18 at 23:09
Welcome to StackOverflow! It's required when you ask that you've attempted to resolve your query and posted the results of that, that you've done a bit of research, and that you clearly let us know what the result is that you're looking for. Please see stackoverflow.com/help/how-to-ask
– zfrisch
Nov 14 '18 at 23:12
Related: stackoverflow.com/questions/16576983/…
– Spencer Wieczorek
Nov 14 '18 at 23:16