最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - replace Dynamic string template variable with json object - Stack Overflow

programmeradmin4浏览0评论

I am having sample template like below i want to replace school, babydet.name,dept,babydet.section.secname with json object values how to do this please help me to solve this.

var Template1 = 'Wele to the {{school}} baby {{babydet.name}}'
var Template2 = 'Wele to the school {{school}} department {{dept}} babydetails {{babydet.name}} {{babydet.section.secname}}'

const namelist1= [{school:'GOVSchool',babydet: { name: 'shanker' }}];
const namelist2= [{school:'GOVSchool',dept:'CS',babydet: { name: 'shanker',section:{sectname:'A Section'}}}];

with help of string replace need to achieve this(single logic). Output want like this "Wele to the world baby shanker"

I am having sample template like below i want to replace school, babydet.name,dept,babydet.section.secname with json object values how to do this please help me to solve this.

var Template1 = 'Wele to the {{school}} baby {{babydet.name}}'
var Template2 = 'Wele to the school {{school}} department {{dept}} babydetails {{babydet.name}} {{babydet.section.secname}}'

const namelist1= [{school:'GOVSchool',babydet: { name: 'shanker' }}];
const namelist2= [{school:'GOVSchool',dept:'CS',babydet: { name: 'shanker',section:{sectname:'A Section'}}}];

with help of string replace need to achieve this(single logic). Output want like this "Wele to the world baby shanker"

Share Improve this question asked Mar 6, 2017 at 3:15 user7620655user7620655 412 silver badges4 bronze badges 1
  • 1 JSON is a textual notation for data exchange. (More.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Those are just objects. – T.J. Crowder Commented Mar 6, 2017 at 3:25
Add a ment  | 

4 Answers 4

Reset to default 4

If by any chance that lodash.js is available, you can use the _.template function for it. More details here.

var Template1 = 'Wele to the {{school}} baby {{babydet.name}}'
var Template2 = 'Wele to the school {{school}} department {{dept}} babydetails {{babydet.name}} {{babydet.section.secname}}'

var namelist1= [{school:'GOVSchool',babydet: { name: 'shanker' }}];
var namelist2= [{school:'GOVSchool',dept:'CS',babydet: { name: 'shanker',section:{sectname:'A Section'}}}];

_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var interpolator = _.template(Template1);
var interpolated = interpolator(namelist2[0]);

console.log(interpolated)

interpolator = _.template(Template2);
interpolated = interpolator(namelist2[0]);

console.log(interpolated)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

You could potentially use ES6 Template literals to interpolate your strings. I'm going to make some basic assumptions (and hard-code them for this example) in order to only answer the question you asked: your namelist vars are stand-ins for some object parsed from JSON data, you know its structure, and you know how to iterate over a data array to make this solution real world useful.

Template literals are enclosed with backticks "`" instead of quotes, and you surround your variables with ${/* reference variable */}

const namelist1= [{school:'GOVSchool',babydet: { name: 'shanker' }}];
const namelist2= [{school:'GOVSchool',dept:'CS',babydet: { name: 'shanker',section:{sectname:'A Section'}}}];

let Template1 = `Wele to the ${namelist1[0].school} baby ${namelist1[0].babydet.name}`;
let Template2 = `Wele to the school ${namelist2[0].school} department ${namelist2[0].dept} babydetails ${namelist2[0].babydet.name} ${namelist2[0].babydet.section.sectname}`;

You can this library for string interpolation. https://www.npmjs./package/string-format.String::format is a small JavaScript library for formatting strings, based on Python's str.format().

I was doing something similar, the only difference is that my JSON object would not have nested properties.

I am using a simple approach (hopefully not too simplistic). It can accept any number of properties to be substituted in the HTML template.

Given a template:

let template =
  '<div><span>{{ text }}</span><span>{{description}}</span></div>';

And some data:

let data = { text: 'My Text', description: 'my description' };

I would get:

<div><span>My Text</span><span>my description</span></div>

See the implementation in JSBin here: https://jsbin./zelogof/7/edit?js,console

Better suggestions also wele!

发布评论

评论列表(0)

  1. 暂无评论