0

I have a function which takes values from two classes and maps it into an array. The array then gets pushed into a send email function.

    var p1 = document.getElementsByClassName('emailtest'),
        email = [].map.call(p1, function(email) {
            return email.value;
        }).join(',');

    var p2 = document.getElementsByClassName('reciptest'),
        rname = [].map.call(p2, function(rname) {
            return rname.value;
        }).join(',');

var to = [];
    var p3 = email.split(',');
    var p4 = rname.split(',');

    p3.forEach(function(em, i) {
        var recipient = {
            email: em,
            name: null,
            type: 'to'
        };
        if (p4.length > i)
            recipient = p4[i];

        to.push(recipient);
    });

How would I implement this in python and more specifically django ? I have a rough idea on splitting the strings but am not sure how to convert the last section p3.forEach(function(em, i) and to.push(recipient)

1 Answer 1

1

Try this out:

to = []
p3 = email.split(',')
p4 = rname.split(',')
for i,em in enumerate(p3):
    recipient = {'email': em, 'name': None, 'type': to}
    if len(p4) > i:
        recipient = p4[i]
    to.append(recipient)
Sign up to request clarification or add additional context in comments.

4 Comments

Hey thanks. In regards to the string split. Would it be best to use request.POST.getlist ?
Only if the POST data has already been split for you. If it comes in as a single comma-delimited string, then no, you have to split it yourself.
GET is probably the easiest
Ok . This is actually a part of a broader issue that Im trying to solve. stackoverflow.com/questions/31329834/… Would really appreciate some advice on this

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.