viviosoft Posted March 10, 2013 Share Posted March 10, 2013 Hello - I'm creating a source code generator and have the following code that loops over an array that creates an object. I want to add a , (comma) to the end of each iteration. Here's what the final output should look like: I have everything generating successfully but adding the , (comma) at the end of each item in the object. var rules1 = { 'fullName' : { required: true }, 'username' : { required: true, email: true }, 'password' : { required: true, minlength: 6, maxlength: 20 } }; How would I iterate over the object and add that comma? Also notice that the last item doesn't have a comma. Here's the source code I have to build the object: var rulesArray = []; $(".InputRulesSection").each(function () { $('.rulesTable').each(function () { var ruleInputName = $('.fieldNameInput', this).val(); var minLength = $('.minLengthInput', this).val(); var maxLength = $('.maxLengthInput', this).val(); var email = $(".email", this).attr("rel"); rulesArray.push( { ruleInputName:ruleInputName, minLengthInput:minLength, maxLengthInput:maxLength, email:email } ); }); }); return rulesArray; Here's the code I have to iterate over the newly created object: for (var i in rulesArray) { var ruleInputVal = rulesArray[i].ruleInputName; var email = rulesArray[i].email; var minLength = rulesArray[i].minLengthInput; var maxLength = rulesArray[i].maxLengthInput; rulesCode += "\t\t '" + ruleInputVal + "' : { \n "; rulesCode += "\t\t\t required: true" + addComma + "\n "; if (email == "email") { rulesCode += "\t\t\t email: true" + addComma + " \n "; } if (minLength > 0) { rulesCode += "\t\t\t minLength: " + minLength + addComma + " \n "; } if (maxLength > 0) { rulesCode += "\t\t\t maxLength: " + maxLength + addComma + " \n "; } rulesCode += "\t\t } \n"; } And finally, here's what I currently generate using the above code: Notice there aren't any commas after each item and keys? That's what I'm stumped on... how to approach it? Any help would be great. Thank you! var rules = { 'username' : { required: true email: true minLength: 6 maxLength: 20 } 'password' : { required: true minLength: 6 maxLength: 20 } }; Link to comment https://forums.phpfreaks.com/topic/275469-loop-through-array-appending-data-to-each-iteration/ Share on other sites More sharing options...
haku Posted March 11, 2013 Share Posted March 11, 2013 I would recommend against this, as adding the comma to the end of an object invalidates it, and IE will throw up an error when this happens. Link to comment https://forums.phpfreaks.com/topic/275469-loop-through-array-appending-data-to-each-iteration/#findComment-1417905 Share on other sites More sharing options...
viviosoft Posted March 11, 2013 Author Share Posted March 11, 2013 I'm sorry... I don't undertand... are you saying that this is correct way to build an object? I think it's missing some commas? var rules = { 'username' : { required: true email: true minLength: 6 maxLength: 20 } 'password' : { required: true minLength: 6 maxLength: 20 } }; Link to comment https://forums.phpfreaks.com/topic/275469-loop-through-array-appending-data-to-each-iteration/#findComment-1418080 Share on other sites More sharing options...
haku Posted March 12, 2013 Share Posted March 12, 2013 Sorry, only the last comma will make it invalid. I hadn't realized you didn't have any of the other commas as well. Change this line of code: for (var i in rulesArray) { To this: addComma = ','; for (var i in rulesArray) { This isn't actually the ideal way to do it, but it will work. Link to comment https://forums.phpfreaks.com/topic/275469-loop-through-array-appending-data-to-each-iteration/#findComment-1418110 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.