循环字符串,向字符串添加单词,返回字符串
我有一个明文对象。我也有一系列对象。在那个对象数组中,每个对象都包含偏移键和长度键。
我想在我的明文字符串上循环,将适当的单词插入某些偏移的字符串中,然后返回该字符串。
代码
const plainText = "Hey name your food is ready, , your food has your name on it"
这是我在下面的下面预期输出的
mergeValues = [
{message: "Hey Keith your salmon is ready, your food has your name on it"},
{message: "Hey Kelly your pizza is ready, your food has your name on it"},
{message: "Hey Ed your hamburger is ready, your food has your name on it"},
{message: "Hey Shelby your sushi is ready, your food has your name on it"}
]
const people = [
{ name: "keith", food: "salmon"},
{ name: "kelly", food: "pizza"},
{ name: "ed", food: "hamburger"},
{ name: "shelby", food: "sushi"}
]
const locations = [
{offset: 4, length: 4, field: 'name'},
{offset: 13, length: 4, field: 'food'}
]
,我正在映射人们,创建一个对象,然后在位置运行一个foreach,最后将该对象返回地图,以使其再次在People Array中的下一个人上运行。 我很确定我的主要问题是我每次在foreach循环中都在对象上重写,而不是修改字符串,保存该值,然后再次修改该字符串,保存值等.....
const mergeValues = people.map((person) => {
const messageObj = {message: ""};
locations.forEach((location) => {
if(Object.keys(person).includes(location.field)) {
messageObj.message = plainText.substring(0, location.offset + 1) + person[location.field] + plainText.substring(location.length + location.offset + 1, plainText.length)
}
})
return messageObj
I have a plainText object. I also have an array of objects. In that array of objects, each object contains offset and length keys.
I want to loop over my plainText string, insert the proper words into the string at certain offsets and then return that string.
Here is my code below
const plainText = "Hey name your food is ready, , your food has your name on it"
expected output below
mergeValues = [
{message: "Hey Keith your salmon is ready, your food has your name on it"},
{message: "Hey Kelly your pizza is ready, your food has your name on it"},
{message: "Hey Ed your hamburger is ready, your food has your name on it"},
{message: "Hey Shelby your sushi is ready, your food has your name on it"}
]
const people = [
{ name: "keith", food: "salmon"},
{ name: "kelly", food: "pizza"},
{ name: "ed", food: "hamburger"},
{ name: "shelby", food: "sushi"}
]
const locations = [
{offset: 4, length: 4, field: 'name'},
{offset: 13, length: 4, field: 'food'}
]
Here I am mapping over people, creating an object, then running a forEach on locations, and finally returning that object back to the map to let it run again on the next person in people array.
Im pretty sure my main issue is that I am Rewriting over the object everytime in the forEach loop instead of modifying the string, saving that value, then modifying that string again, saving value etc.....
const mergeValues = people.map((person) => {
const messageObj = {message: ""};
locations.forEach((location) => {
if(Object.keys(person).includes(location.field)) {
messageObj.message = plainText.substring(0, location.offset + 1) + person[location.field] + plainText.substring(location.length + location.offset + 1, plainText.length)
}
})
return messageObj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您可以更改使用自定义vars的明文字符串,则可以创建自己的函数以使用
此方法的正则表达式填充这些var,甚至可以在多个位置放置相同的变量,您可以使用自己的语法控制变量位置(<<<<代码> {varname} 在这种情况下)。
较少的复杂性,您不需要计算位置,该方法将使添加新的Vars更加困难。
将编译为
仅将
{name}
添加到模板codesandbox: https://codesandbox.io/s/loving-cherry-jnwt5s?file=/src/index.js
If you can change the plainText string to use custom vars, you can create your own function to fill those vars using regular expressions
With this approach you can even put the same variable in multiple places, you control the variable location using your own syntaxs (
{varName}
in this case).Less complexity, you don't need to count positions, that approach will make adding new vars harder.
Will compile to
just adding
{name}
to the templateCodeSandbox: https://codesandbox.io/s/loving-cherry-jnwt5s?file=/src/index.js
您的解决方案非常复杂,请尝试使用 String.replace,如下所示:
Your solution is pretty complex, try with String.replace, like this:
您只需使用
map
,
减少
和替换
以实现结果为:You can simply use
map
,reduce
andreplace
to achieve the result as:你可以用替换来尝试一下。
You can try this with replace.