[英]Replace affects all strings in an array
I am having some issues about replacing. 我在更换时遇到一些问题。 I am fetching some integers from DB. 我正在从数据库中获取一些整数。 And if the number fetched is just 1 then I replace it with "empty", but when I do that, it affects all numbers that have 1 in them. 如果获取的数字仅为 1,则我将其替换为“空”,但是当我这样做时,它将影响其中所有包含1的数字。
Here is what I am doing, first calling toString
and then replace
这是我在做什么,首先调用toString
然后replace
<tr v-if="moreIndex < one.length" v-for="moreIndex in morePro"> <td>{{one[moreIndex].products}}</td> <td>{{priceSep(one[moreIndex].price).toString().replace("1", "empty")}}</td> </tr>
So I am changing "1" to empty but if any other data has includes 1 then output look like this... 所以我将“ 1”更改为空,但是如果任何其他数据包含“ 1”,则输出看起来像这样...
5empty98
How can I fix this problem? 我该如何解决这个问题?
You can check the length with Conditional (ternary) operator .: 您可以使用条件(三元)运算符 。:检查长度 。
<td>{{priceSep(one[moreIndex].price).toString().length == 1 ? priceSep(one[moreIndex].price).toString().replace("1", "empty") : priceSep(one[moreIndex].price).toString()}}</td>
May be, you can try directly set the value if the value is 1: 可能是,如果值为1,则可以尝试直接设置该值:
<td>{{priceSep(one[moreIndex].price).toString() == "1" ? "empty" : priceSep(one[moreIndex].price).toString()}}</td>
You can check the length of the string before replacing it 您可以在替换之前检查字符串的长度
{{priceSep(one[moreIndex].price).toString().length > ?
priceSep(one[moreIndex].price).toString() :
priceSep(one[moreIndex].price).toString().replace("1", "empty")
}}
您可以使用RegExp:
{{priceSep(one[moreIndex].price).toString().replace(/^1$/, "empty")}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.