简体   繁体   English

Javascript 动态字符串模板到标题

[英]Javascript dynamic string template to title

I have a string template that backend sends dynamically.我有一个后端动态发送的字符串模板。 Each table tab has a different label_pattern when user want to edit or delete a row we should build the title based on this pattern.. At the moment we use the hash id in the title (pop-up, when want to edit or delete a table row) but it is not descriptive..每个表格选项卡都有不同的 label_pattern 当用户想要编辑或删除一行时,我们应该基于此模式构建标题。目前我们在标题中使用 hash id(弹出,当想要编辑或删除一个表行)但它不是描述性的..

The string template look like this:字符串模板如下所示:

label_pattern: "{{code}}, {{description}}, {{category}}"

We can also have access through props to the row we want to delete or edit and the object looks like this:我们还可以通过 props 访问我们要删除或编辑的行,object 如下所示:

{
  category: "Fruit"
  code: "AP"
  description: "Green Apple"
  hash: "b45516ddda8566ee"
}

I used the split function to create an array from the label pattern:我使用拆分 function 从 label 模式创建一个数组:

 ['{{code}},', '{{description}},', '{{category}},']

The reason why it is important because the label_pattern on a different table tab maybe looks differently.之所以重要,是因为不同表选项卡上的 label_pattern 可能看起来不同。 So we always have to compare the active table pattern because they adjusted to the current tab's row object.所以我们总是必须比较活动表模式,因为它们调整到当前选项卡的行 object。 I can access the active tab pattern and the row's data too.我也可以访问活动选项卡模式和行数据。 I just don't know how to iterate, map and replace through the pattern array in order to build the title text based on this..我只是不知道如何迭代,map 并通过模式数组替换,以便基于此构建标题文本..

You can use a regex and a replacer function to replace the text with the objects properies:您可以使用正则表达式和替换器 function 将文本替换为对象属性:

 const obj = { category: "Fruit", code: "AP", description: "Green Apple", hash: "b45516ddda8566ee", }; const label_pattern = "{{code}}, {{description}}, {{category}}"; let label = label_pattern.replace(/{{(?<prop>\w+)}}/g, function(match, p, offset, string, groups) { return obj[groups.prop]; }); console.log(label);

I think this should do the trick:我认为这应该可以解决问题:

let label_pattern = "{{code}}, {{description}}, {{category}}".split(",");
let label_array = label_pattern.map((value) => { return value.trim().substring(2, value.trim().length-2) });

This considers your input string will always be in this format: "{{label1}}, {{label2}}, {{label3}}..."这认为您的输入字符串将始终采用以下格式:“{{label1}}, {{label2}}, {{label3}}...”

Explanation: I split the string with commas as you mentioned, then map each label we have and remove any trailing spaces.说明:我用逗号分隔字符串,如您所提到的,然后 map 每个 label 我们拥有并删除任何尾随空格。 Then we find the substring in each string removing two characters from front and back.然后我们在每个字符串中找到 substring 删除前后两个字符。

Edit: Sorry for the bad indentation and formatting, I solved it in the console.编辑:抱歉缩进和格式不好,我在控制台中解决了它。

Edit 2: In order to extract the values from an object whose keys are as a string in the array (label_array in our case) the following code will work:编辑 2:为了从 object 中提取值,其键是数组中的字符串(在我们的例子中为 label_array),下面的代码将起作用:

let obj = {
  "category": "Fruit",
  "code": "AP",
  "description": "Green Apple",
  "hash": "b45516ddda8566ee"
}

let values = label_array.map((key) => { return obj[key]});

In my execution the values was: ['AP', 'Green Apple', 'Fruit']在我的执行中,值是:['AP', 'Green Apple', 'Fruit']

You can simply achieve it by using RegEx with the help of String.replaceAll() method.您可以在String.replaceAll()方法的帮助下使用RegEx来简单地实现它。

Live Demo :现场演示

 const label_pattern = "{{code}}, {{description}}, {{category}}"; const obj = { category: "Fruit", code: "AP", description: "Green Apple", hash: "b45516ddda8566ee" }; const reg = /{{|}}/g const splittedStr = label_pattern.replaceAll(reg, '').split(','); function buildLabel(splittedStr, obj) { const label = splittedStr.map(item => obj[item.trim()]); return label.join(' ') } console.log(buildLabel(splittedStr, obj));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM