Some outside code is giving me a string value like..
null,402,2912,2909,2910,2913,2911,2914,2915,2388,2389,2390,
now i have to save this value to the data base but putting 0 in place of null in javascript. Is there any javascript string releated function to do this conversion?
You can simply use the
replacefunction over and over again until all instances are replaced, but make sure that all your string will ever contain is the character sequencenullor a number (and obviously the delimiting comma):You need to run a
forloop because the functionString.replace(search, rplc)will replace only the first instance ofsearchwithrplc. So we use theindexOfmethod to check, in each iteration, if the required term exists or not. Another alternative (and in my opinion, a better alternative would be:Basically, what we are doing is that since you will anyways be converting this to an array of numbers (I presume, and sort of hope), we first split it into individual elements and then inspect each element to see if it is
nulland make the conversion accordingly.