Jump to content

Recommended Posts

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 
 		 } 
	 }; 

Edited by viviosoft

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 
 		 } 
	 };

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.

Edited by haku
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.