admin管理员组文章数量:1334198
I'd like to do the following
var obj = {
animal: "${animal}"
};
var res = magic(obj, {animal: "cat"});
// res => {animal: "cat"}
magic
is some function that does the dirty work. Obviously obj
could be more plex with multiple keys, nested arrays and so on. The template variable could be inside an array like this
var obj = {
animals: ["cat", "dog", "${animal}", "cow"]
};
and it could be anywhere in the array so simply doing obj.animals[2] = "bat";
isn't feasible.
I've found the underscore-tpl library with which I can achieve what I want, but I would like to know if there are other solutions for future reference and because I had a hard time finding underscore-tpl in the first place.
My actual use is case is that I have a config.json file where I have several declarations like the following
{
"task1": {
"mand": "mand-line-program",
"args": [
"--input", "{{input}}",
"--flag1",
"--output", "{{output}}",
"--flag2",
],
"options": {
"cwd": "path-to-working-dir"
}
}
}
I parse this consig.json using JSON.parse(...)
and I call require("child_process").spawn
with the mand
, args
and options
parameters declared in the file, however args
change a lot, flags added, reordered and stuff, so simply doing config.task1.args[1] = "<input value>";
involves changing the code that invokes spawn
and this is as error prone as it gets.
Update
Based on the accepted answer I've created a simple package (located here) which I can include in my projects, feel free to use it.
I'd like to do the following
var obj = {
animal: "${animal}"
};
var res = magic(obj, {animal: "cat"});
// res => {animal: "cat"}
magic
is some function that does the dirty work. Obviously obj
could be more plex with multiple keys, nested arrays and so on. The template variable could be inside an array like this
var obj = {
animals: ["cat", "dog", "${animal}", "cow"]
};
and it could be anywhere in the array so simply doing obj.animals[2] = "bat";
isn't feasible.
I've found the underscore-tpl library with which I can achieve what I want, but I would like to know if there are other solutions for future reference and because I had a hard time finding underscore-tpl in the first place.
My actual use is case is that I have a config.json file where I have several declarations like the following
{
"task1": {
"mand": "mand-line-program",
"args": [
"--input", "{{input}}",
"--flag1",
"--output", "{{output}}",
"--flag2",
],
"options": {
"cwd": "path-to-working-dir"
}
}
}
I parse this consig.json using JSON.parse(...)
and I call require("child_process").spawn
with the mand
, args
and options
parameters declared in the file, however args
change a lot, flags added, reordered and stuff, so simply doing config.task1.args[1] = "<input value>";
involves changing the code that invokes spawn
and this is as error prone as it gets.
Update
Based on the accepted answer I've created a simple package (located here) which I can include in my projects, feel free to use it.
Share Improve this question edited May 23, 2017 at 10:29 CommunityBot 11 silver badge asked Jul 30, 2015 at 16:07 Kohányi RóbertKohányi Róbert 10.2k4 gold badges56 silver badges83 bronze badges 3-
1
Why not just
animal: res.animal
? If you need the interpolation in the middle of a larger string, checkout ES2015 template strings. edit oh OK I see what you're getting at. – Pointy Commented Jul 30, 2015 at 16:12 - Look for JS template engines. Note that asking for off-site resources, libraries, etc. is actually OT for SO. – Dave Newton Commented Jul 30, 2015 at 17:08
- @DaveNewton Well I admit this isn't the best all time SO question :) I really just wanted to put something "out there", so that I (and others) could find it more easily, because I had a hard time finding a painless solution for this. – Kohányi Róbert Commented Jul 30, 2015 at 20:26
2 Answers
Reset to default 2You could JSON.stringify
the object, then replace your search value with the actual value, then JSON.parse
the result:
function magic(o, a) {
var j = JSON.stringify(o);
for (var k in a) {
j = j.split('${'+k+'}').join(a[k]);
}
return JSON.parse(j);
}
I suggest to you a very simple but very fast and understandable template engine: simple-template.js
It consists of 22 lines of code, very simple! Considering that, you will be able to render your config easily!
本文标签: jsonWays to interpolate template variables inside JavaScript objectsStack Overflow
版权声明:本文标题:json - Ways to interpolate template variables inside JavaScript objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742331437a2454774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论