3

I need the following object:

var myObj={
    "rules": {
        "email": {
            "required": true,
            "email": true,
            "remote": {
                "url": "check-email.php",
                "type": "post",
                "data": {
                    "someName1": function() {
                        return $( '#someID1' ).val();
                    },             
                    "someName2": function() {
                        return $( '#someID2' ).val();
                    },             
                    "someName3": "bla"
                }
            }
        }
    }
};

To create it, I only have some JSON such as the following:

{
    "rules": {
        "email": {
            "required": true,
            "email": true,
            "remote": {
                "url": "check-email.php",
                "type": "post",
                "data": {
                    "someName1": {"id":"someID1"},             
                    "someName2": {"id":"someID2"},             
                    "someName3": "bla"
                }
            }
        }
    }
}

or

{
    "rules": {
        "email": {
            "required": true,
            "email": true,
            "remote": {
                "url": "check-email.php",
                "type": "post",
                "data": {
                    "someName1": {"function":"return $( '#someID1' ).val()"},             
                    "someName2": {"function":"return $( '#someID2' ).val()"},             
                    "someName3": "bla"
                }
            }
        }
    }
}

or something similar.

How can this be accomplished?

2 Answers 2

3

You can use Function to create functions on the fly. Supposing you have the second JSON structure:

d = obj.rules.email.remote.data;
Object.keys(d).forEach(function(key) {
    d[key] = new Function(d[key].function); 
    //Note: You might want to check if d[key].function exists before using it.
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Thrustmaster. I will test it out.
Also works. jsfiddle.net/e4hq2w1b/4 or without creating a new variable jsfiddle.net/e4hq2w1b/5. Note that I was unable to use function as a property name.
2

For this pattern :

{
    "rules": {
        "email": {
            "required": true,
            "email": true,
            "remote": {
                "url": "check-email.php",
                "type": "post",
                "data": {
                    "someName1": {"id":"someID1"},             
                    "someName2": {"id":"someID2"},             
                    "someName3": "bla"
                }
            }
        }
    }
}

Can be done as:

for(var key in myObj.rules.email.remote.data){
    if(myObj.rules.email.remote.data[key].id){
    myObj.rules.email.remote.data[key] = function(){
          return $('#'+myObj.rules.email.remote.data[key].id).val();
        }
    }
}

if email is not hardcoded then it can be done as:

for(var key1 in myObj.rules){
 for(var key in myObj.rules[key1].remote.data){
        if(myObj.rules[key1].remote.data[key].id){
        myObj.rules[key1].remote.data[key] = function(){
              return $('#'+myObj.rules[key1].remote.data[key].id).val();
            }
        }
    }
}

5 Comments

My bad for not being more specific. What if email can't be hardcoded, and the same should happen to any with a similar pattern? Pattern being *.rules.*.remote.data.
Thanks Gaurav, Let me try it out.
Thanks weird. console.log shows rules.email.remote.data.someName1 as a function even before it is converted??? Still testing. Thanks jsfiddle.net/e4hq2w1b/1
Doesn't work: jsfiddle.net/e4hq2w1b/2. But then changed to declare id is a variable and it does: jsfiddle.net/e4hq2w1b/3. Do you know why the difference?
Actually, the later example still doesn't work as it always returns the value of the last (i.e. will return the value in #someID2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.