Create string template and replace with contents of array
Say I have a string like so:
const m = 'Map<?,?>';
and an array like so:
const types = ['string', 'boolean'];
and I am looking to generate:
const r = 'Map<string,boolean>'
how can I do that? Note that I don't need to use a question mark in the template string, I can use a different character, maybe like this:
const m = 'Map<%s,%s>';
Also please note that I cannot use ES6 template strings of this nature ${}
because my template is not wrapped in a function.
I believe this will work in Node.js:
util.format(m, ...types);
but I guess what I am looking for is a way to use a question mark character (?) instead of using %s.
javascript node.js
add a comment |
Say I have a string like so:
const m = 'Map<?,?>';
and an array like so:
const types = ['string', 'boolean'];
and I am looking to generate:
const r = 'Map<string,boolean>'
how can I do that? Note that I don't need to use a question mark in the template string, I can use a different character, maybe like this:
const m = 'Map<%s,%s>';
Also please note that I cannot use ES6 template strings of this nature ${}
because my template is not wrapped in a function.
I believe this will work in Node.js:
util.format(m, ...types);
but I guess what I am looking for is a way to use a question mark character (?) instead of using %s.
javascript node.js
add a comment |
Say I have a string like so:
const m = 'Map<?,?>';
and an array like so:
const types = ['string', 'boolean'];
and I am looking to generate:
const r = 'Map<string,boolean>'
how can I do that? Note that I don't need to use a question mark in the template string, I can use a different character, maybe like this:
const m = 'Map<%s,%s>';
Also please note that I cannot use ES6 template strings of this nature ${}
because my template is not wrapped in a function.
I believe this will work in Node.js:
util.format(m, ...types);
but I guess what I am looking for is a way to use a question mark character (?) instead of using %s.
javascript node.js
Say I have a string like so:
const m = 'Map<?,?>';
and an array like so:
const types = ['string', 'boolean'];
and I am looking to generate:
const r = 'Map<string,boolean>'
how can I do that? Note that I don't need to use a question mark in the template string, I can use a different character, maybe like this:
const m = 'Map<%s,%s>';
Also please note that I cannot use ES6 template strings of this nature ${}
because my template is not wrapped in a function.
I believe this will work in Node.js:
util.format(m, ...types);
but I guess what I am looking for is a way to use a question mark character (?) instead of using %s.
javascript node.js
javascript node.js
asked Nov 21 '18 at 22:35
MrCholoMrCholo
1,408933
1,408933
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The smallest way is probably using a combination of reduce
and replace
.
Replace only replaces the first found position and reduce makes it easy to iterate over the given replacements.
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
const n = types.reduce((n, t) => n.replace('?', t), m);
console.log(n);
Or to formalize it into a function:
function format(str, key, replacements){
return replacements.reduce((s, t) => s.replace(k, t), str);
}
probably a good idea to make a note on the comma operator, or explain that in the comments here. Oh just kidding, that's not a comma operator.
– MrCholo
Nov 21 '18 at 22:42
What do you mean by comma operator?
– Sebastian Speitel
Nov 21 '18 at 22:42
I misread your code, but this is the comma op: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– MrCholo
Nov 21 '18 at 22:43
Is anything else unclear?
– Sebastian Speitel
Nov 21 '18 at 22:44
1
This is probably the best answer possible. I'm just miffed that I never thought of iterating over a string like this, and especially in a way that works with aconst
string. Nice answer!
– zfrisch
Nov 21 '18 at 22:52
|
show 10 more comments
You can also use replace()
directly with a little fancy closure work to capture the index (which sadly isn't passed to the replace callback function):
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
let rep = m.replace(/?/g, ((i) => () => types[i++])(0)) // i is simple a count of matches starting at 0
console.log(rep)
If the immediately-executed function is too weird, you can wrap the whole thing as a simple function that's a little easier on the eyes:
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
function replaceMatch(str, arr){
let i = 0
return str.replace(/?/g, () => arr[i++])
}
console.log(replaceMatch(m, types))
1
Interesting answer! That is one weird looking closure
– zfrisch
Nov 21 '18 at 23:01
add a comment |
You only need one ?
const types = ['string', 'boolean'];
console.log('Map<?>'.split('?').join(types))
or
const types = ['string', 'boolean'];
console.log('Map<?>'.replace('?', types))
or if you wished to keep ?,?
const types = ['string', 'boolean'];
console.log('Map<?,?>'.replace('?,?', types))
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%2f53421388%2fcreate-string-template-and-replace-with-contents-of-array%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
The smallest way is probably using a combination of reduce
and replace
.
Replace only replaces the first found position and reduce makes it easy to iterate over the given replacements.
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
const n = types.reduce((n, t) => n.replace('?', t), m);
console.log(n);
Or to formalize it into a function:
function format(str, key, replacements){
return replacements.reduce((s, t) => s.replace(k, t), str);
}
probably a good idea to make a note on the comma operator, or explain that in the comments here. Oh just kidding, that's not a comma operator.
– MrCholo
Nov 21 '18 at 22:42
What do you mean by comma operator?
– Sebastian Speitel
Nov 21 '18 at 22:42
I misread your code, but this is the comma op: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– MrCholo
Nov 21 '18 at 22:43
Is anything else unclear?
– Sebastian Speitel
Nov 21 '18 at 22:44
1
This is probably the best answer possible. I'm just miffed that I never thought of iterating over a string like this, and especially in a way that works with aconst
string. Nice answer!
– zfrisch
Nov 21 '18 at 22:52
|
show 10 more comments
The smallest way is probably using a combination of reduce
and replace
.
Replace only replaces the first found position and reduce makes it easy to iterate over the given replacements.
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
const n = types.reduce((n, t) => n.replace('?', t), m);
console.log(n);
Or to formalize it into a function:
function format(str, key, replacements){
return replacements.reduce((s, t) => s.replace(k, t), str);
}
probably a good idea to make a note on the comma operator, or explain that in the comments here. Oh just kidding, that's not a comma operator.
– MrCholo
Nov 21 '18 at 22:42
What do you mean by comma operator?
– Sebastian Speitel
Nov 21 '18 at 22:42
I misread your code, but this is the comma op: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– MrCholo
Nov 21 '18 at 22:43
Is anything else unclear?
– Sebastian Speitel
Nov 21 '18 at 22:44
1
This is probably the best answer possible. I'm just miffed that I never thought of iterating over a string like this, and especially in a way that works with aconst
string. Nice answer!
– zfrisch
Nov 21 '18 at 22:52
|
show 10 more comments
The smallest way is probably using a combination of reduce
and replace
.
Replace only replaces the first found position and reduce makes it easy to iterate over the given replacements.
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
const n = types.reduce((n, t) => n.replace('?', t), m);
console.log(n);
Or to formalize it into a function:
function format(str, key, replacements){
return replacements.reduce((s, t) => s.replace(k, t), str);
}
The smallest way is probably using a combination of reduce
and replace
.
Replace only replaces the first found position and reduce makes it easy to iterate over the given replacements.
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
const n = types.reduce((n, t) => n.replace('?', t), m);
console.log(n);
Or to formalize it into a function:
function format(str, key, replacements){
return replacements.reduce((s, t) => s.replace(k, t), str);
}
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
const n = types.reduce((n, t) => n.replace('?', t), m);
console.log(n);
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
const n = types.reduce((n, t) => n.replace('?', t), m);
console.log(n);
edited Nov 21 '18 at 22:51
answered Nov 21 '18 at 22:40
Sebastian SpeitelSebastian Speitel
4,5102525
4,5102525
probably a good idea to make a note on the comma operator, or explain that in the comments here. Oh just kidding, that's not a comma operator.
– MrCholo
Nov 21 '18 at 22:42
What do you mean by comma operator?
– Sebastian Speitel
Nov 21 '18 at 22:42
I misread your code, but this is the comma op: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– MrCholo
Nov 21 '18 at 22:43
Is anything else unclear?
– Sebastian Speitel
Nov 21 '18 at 22:44
1
This is probably the best answer possible. I'm just miffed that I never thought of iterating over a string like this, and especially in a way that works with aconst
string. Nice answer!
– zfrisch
Nov 21 '18 at 22:52
|
show 10 more comments
probably a good idea to make a note on the comma operator, or explain that in the comments here. Oh just kidding, that's not a comma operator.
– MrCholo
Nov 21 '18 at 22:42
What do you mean by comma operator?
– Sebastian Speitel
Nov 21 '18 at 22:42
I misread your code, but this is the comma op: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– MrCholo
Nov 21 '18 at 22:43
Is anything else unclear?
– Sebastian Speitel
Nov 21 '18 at 22:44
1
This is probably the best answer possible. I'm just miffed that I never thought of iterating over a string like this, and especially in a way that works with aconst
string. Nice answer!
– zfrisch
Nov 21 '18 at 22:52
probably a good idea to make a note on the comma operator, or explain that in the comments here. Oh just kidding, that's not a comma operator.
– MrCholo
Nov 21 '18 at 22:42
probably a good idea to make a note on the comma operator, or explain that in the comments here. Oh just kidding, that's not a comma operator.
– MrCholo
Nov 21 '18 at 22:42
What do you mean by comma operator?
– Sebastian Speitel
Nov 21 '18 at 22:42
What do you mean by comma operator?
– Sebastian Speitel
Nov 21 '18 at 22:42
I misread your code, but this is the comma op: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– MrCholo
Nov 21 '18 at 22:43
I misread your code, but this is the comma op: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– MrCholo
Nov 21 '18 at 22:43
Is anything else unclear?
– Sebastian Speitel
Nov 21 '18 at 22:44
Is anything else unclear?
– Sebastian Speitel
Nov 21 '18 at 22:44
1
1
This is probably the best answer possible. I'm just miffed that I never thought of iterating over a string like this, and especially in a way that works with a
const
string. Nice answer!– zfrisch
Nov 21 '18 at 22:52
This is probably the best answer possible. I'm just miffed that I never thought of iterating over a string like this, and especially in a way that works with a
const
string. Nice answer!– zfrisch
Nov 21 '18 at 22:52
|
show 10 more comments
You can also use replace()
directly with a little fancy closure work to capture the index (which sadly isn't passed to the replace callback function):
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
let rep = m.replace(/?/g, ((i) => () => types[i++])(0)) // i is simple a count of matches starting at 0
console.log(rep)
If the immediately-executed function is too weird, you can wrap the whole thing as a simple function that's a little easier on the eyes:
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
function replaceMatch(str, arr){
let i = 0
return str.replace(/?/g, () => arr[i++])
}
console.log(replaceMatch(m, types))
1
Interesting answer! That is one weird looking closure
– zfrisch
Nov 21 '18 at 23:01
add a comment |
You can also use replace()
directly with a little fancy closure work to capture the index (which sadly isn't passed to the replace callback function):
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
let rep = m.replace(/?/g, ((i) => () => types[i++])(0)) // i is simple a count of matches starting at 0
console.log(rep)
If the immediately-executed function is too weird, you can wrap the whole thing as a simple function that's a little easier on the eyes:
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
function replaceMatch(str, arr){
let i = 0
return str.replace(/?/g, () => arr[i++])
}
console.log(replaceMatch(m, types))
1
Interesting answer! That is one weird looking closure
– zfrisch
Nov 21 '18 at 23:01
add a comment |
You can also use replace()
directly with a little fancy closure work to capture the index (which sadly isn't passed to the replace callback function):
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
let rep = m.replace(/?/g, ((i) => () => types[i++])(0)) // i is simple a count of matches starting at 0
console.log(rep)
If the immediately-executed function is too weird, you can wrap the whole thing as a simple function that's a little easier on the eyes:
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
function replaceMatch(str, arr){
let i = 0
return str.replace(/?/g, () => arr[i++])
}
console.log(replaceMatch(m, types))
You can also use replace()
directly with a little fancy closure work to capture the index (which sadly isn't passed to the replace callback function):
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
let rep = m.replace(/?/g, ((i) => () => types[i++])(0)) // i is simple a count of matches starting at 0
console.log(rep)
If the immediately-executed function is too weird, you can wrap the whole thing as a simple function that's a little easier on the eyes:
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
function replaceMatch(str, arr){
let i = 0
return str.replace(/?/g, () => arr[i++])
}
console.log(replaceMatch(m, types))
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
let rep = m.replace(/?/g, ((i) => () => types[i++])(0)) // i is simple a count of matches starting at 0
console.log(rep)
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
let rep = m.replace(/?/g, ((i) => () => types[i++])(0)) // i is simple a count of matches starting at 0
console.log(rep)
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
function replaceMatch(str, arr){
let i = 0
return str.replace(/?/g, () => arr[i++])
}
console.log(replaceMatch(m, types))
const m = 'Map<?,?>';
const types = ['string', 'boolean'];
function replaceMatch(str, arr){
let i = 0
return str.replace(/?/g, () => arr[i++])
}
console.log(replaceMatch(m, types))
edited Nov 21 '18 at 23:15
answered Nov 21 '18 at 22:53


Mark MeyerMark Meyer
38.6k33159
38.6k33159
1
Interesting answer! That is one weird looking closure
– zfrisch
Nov 21 '18 at 23:01
add a comment |
1
Interesting answer! That is one weird looking closure
– zfrisch
Nov 21 '18 at 23:01
1
1
Interesting answer! That is one weird looking closure
– zfrisch
Nov 21 '18 at 23:01
Interesting answer! That is one weird looking closure
– zfrisch
Nov 21 '18 at 23:01
add a comment |
You only need one ?
const types = ['string', 'boolean'];
console.log('Map<?>'.split('?').join(types))
or
const types = ['string', 'boolean'];
console.log('Map<?>'.replace('?', types))
or if you wished to keep ?,?
const types = ['string', 'boolean'];
console.log('Map<?,?>'.replace('?,?', types))
add a comment |
You only need one ?
const types = ['string', 'boolean'];
console.log('Map<?>'.split('?').join(types))
or
const types = ['string', 'boolean'];
console.log('Map<?>'.replace('?', types))
or if you wished to keep ?,?
const types = ['string', 'boolean'];
console.log('Map<?,?>'.replace('?,?', types))
add a comment |
You only need one ?
const types = ['string', 'boolean'];
console.log('Map<?>'.split('?').join(types))
or
const types = ['string', 'boolean'];
console.log('Map<?>'.replace('?', types))
or if you wished to keep ?,?
const types = ['string', 'boolean'];
console.log('Map<?,?>'.replace('?,?', types))
You only need one ?
const types = ['string', 'boolean'];
console.log('Map<?>'.split('?').join(types))
or
const types = ['string', 'boolean'];
console.log('Map<?>'.replace('?', types))
or if you wished to keep ?,?
const types = ['string', 'boolean'];
console.log('Map<?,?>'.replace('?,?', types))
const types = ['string', 'boolean'];
console.log('Map<?>'.split('?').join(types))
const types = ['string', 'boolean'];
console.log('Map<?>'.split('?').join(types))
const types = ['string', 'boolean'];
console.log('Map<?>'.replace('?', types))
const types = ['string', 'boolean'];
console.log('Map<?>'.replace('?', types))
const types = ['string', 'boolean'];
console.log('Map<?,?>'.replace('?,?', types))
const types = ['string', 'boolean'];
console.log('Map<?,?>'.replace('?,?', types))
edited Nov 22 '18 at 10:27
answered Nov 21 '18 at 22:42


kemicofakemicofa
10.2k43982
10.2k43982
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%2f53421388%2fcreate-string-template-and-replace-with-contents-of-array%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