Replace single value from text list field Using JS to Bubble

@adamhholmes as I said if this works for the use case then that is great. The only point I was trying to make is that having a production app rely on string value replacement when you can’t be certain that the end user input is sanitized for unique values (which I didn’t see that happening or described in the OP’s example) is problematic.

So you implement this solution and then all of the sudden unexpectedly the user happens to accidentally enter in duplicate ID’s and you have a strange bug being reported and a bad user experience.

There is nothing wrong with the code you provided to complete the task suggested as long as the OP (and any future reader) understands that there are scenarios in which it will fail. Consequently, to use this approach successfully, the input should really be sanitized to ensure no duplicate values can be input by the user.

What I meant by This kind of “programming” is simply that it is a bad idea to just go about trying to replace values in a string list unless you are certain the list contains only unique values. Which, btw is probably one of the reasons why Bubble only allows unique values in their lists (so as to not run into these sort of confusing scenarios).

Edit: And I didn’t test it but I’m quite certain this is going to be the same issue for the Regex scenario. It will replace the first instance or all instances, either of which is probably not the behavior the end user would be expecting.

1 Like

Ok that’s a fair point and, actually, I hadn’t noticed that the original post use-case was using User-input values here…

(as I said, whenever I use this, or similar, I always use object arrays, and ensure each object has a unique ID property, to avoid such issues).

But as the use-case here involves texts entered directly by the User then yes, any solution will need to ensure each text item is unique (or include some other way to deal with duplicate items).

1 Like

Any string based matches without unique identifiers will return multiple matches. It’s even reproducible in the demo page I shared. You’re right to mention it. I just took into account OP’s level of experience.

My own implementation of regex matching works by matching a UID key and then returns it’s attached value. A pseudo JSON. I don’t use JSON for certain processes because JSON is a little trickier to manage in Bubble without plugins or workarounds.