PDA

View Full Version : no comma on the last array



zbphill
10-02-2009, 05:18 PM
I need to figure out how NOT to put a comma on the last array.


images : [
<% for(int i = 0; i< productInfolist.size(); i++){
ProductInfo productInfo = (ProductInfo)productInfolist.get(i);
String familyDescription = productInfo.getFamilyDescription();
familyDescription = DataMethods.findReplace(familyDescription,"\"","\\\"");
familyDescription = familyDescription.replaceAll("(\r\n|\r|\n|\n\r)", " "); %>

["<%=CategoryInfoRetrieve.getProductDetailHeroImage(productInfo.getProduct())%>", "<%=DataMethods.findReplace(productInfo.getProductFamilyName(),"\"","\\\"")%>", "<%=familyDescription%>", "<%=productInfo.getFamilyFormatedPrice(false)%>", "<%=productInfo.getProductURL()%>"],

<% }
%>

],

Thanks
Zach

jscheuer1
10-02-2009, 06:23 PM
Just say 'No.' Er - if this is javascript (where I found this post) that would be the answer. However, it looks like you are also using asp or something server side like asp. If the server code is producing this unwanted comma, that's where you need to address it. If javascript is, and it is a string, the replace method may be used to remove it. If it is an array, an extra comma at the end denotes an additional undefined item, the array may be popped via the pop() method. If it is an object, I'm not aware of any simple way to get rid of it. All in all, it would be best to prevent it from getting there in the first place, unless it is causing no real problem, in which case - forget about it.

By the way, just looking at the code, I'm not even sure which comma you are talking about.

One other thing I should mention, as it may be relevant - if you were to use a regular expression to insert commas at certain points, you could use a negative look ahead to skip placing one at the end.

Also, if you already have a string at some point with delimiters of any kind, even if they are irregular as appears to be the case here:


(\r\n|\r|\n|\n\r)

in javascript the string can then be made into an array by splitting it on those delimiters, ex:


images: (function(s){return s.split(/\r\n|\r|\n|\n\r/);})('string to split'),

zbphill
10-02-2009, 06:39 PM
Thanks John

The javascript is inside a .jsp file.

What I'm getting is


["<%=CategoryInfoRetrieve.getProductDetailHeroImage(productInfo.getProduct())%>", "<%=DataMethods.findReplace(productInfo.getProductFamilyName(),"\"","\\\"")%>", "<% out.print(familyDescription); %>", "<%=productInfo.getFamilyFormatedPrice(false)%>", "<%=productInfo.getProductURL()%>"],
["<%=CategoryInfoRetrieve.getProductDetailHeroImage(productInfo.getProduct())%>", "<%=DataMethods.findReplace(productInfo.getProductFamilyName(),"\"","\\\"")%>", "<% out.print(familyDescription); %>", "<%=productInfo.getFamilyFormatedPrice(false)%>", "<%=productInfo.getProductURL()%>"],


and what i need is


["<%=CategoryInfoRetrieve.getProductDetailHeroImage(productInfo.getProduct())%>", "<%=DataMethods.findReplace(productInfo.getProductFamilyName(),"\"","\\\"")%>", "<% out.print(familyDescription); %>", "<%=productInfo.getFamilyFormatedPrice(false)%>", "<%=productInfo.getProductURL()%>"],
["<%=CategoryInfoRetrieve.getProductDetailHeroImage(productInfo.getProduct())%>", "<%=DataMethods.findReplace(productInfo.getProductFamilyName(),"\"","\\\"")%>", "<% out.print(familyDescription); %>", "<%=productInfo.getFamilyFormatedPrice(false)%>", "<%=productInfo.getProductURL()%>"]

jscheuer1
10-02-2009, 07:08 PM
Yes, but how do you get what you don't want? Something (some code) must be placing the comma there. I don't see, haven't seen yet, in your posts any javascript that could be doing anything like that. The replaceAll method which I see as the most likely candidate for this isn't native javascript code, probably (if it's native anything) it's VB. If it is a custom javascript function, I'd need to see that. If it's VB or some other server code, that's where you need to look to fix it.