1

I'm using find to get values on my variable, but when this variable return multiples values, my condition can't read them.

What I need is sum this variable values, returning just one number.

Code:

mail.forEach(function(m) {

    var nresult = $("#dat").contents()
        .find("td:contains('" + m.hexEncode() + "')" )
        .length;

    return nresult;

});

I need to keep this forEach function - is the only way to keep my external data working, so, this will return, in that case, this values: 0,0,1,1,0, and I need just 2 on return.

How can I do that? Thanks!

2
  • is nresult an array of values? Commented Mar 4, 2016 at 1:57
  • I think it is. I mean, if I use some script deleting the past value and adding a new one, just one value return. But when I use something like append, I get all values returning. Commented Mar 4, 2016 at 2:00

1 Answer 1

1

EDIT (after chat) : You can modify your return statement to do something like that :

var res = mail.map(function(m) { 

    var nresult = $("#dat").contents() 
    .find("td:contains('" + m.hexEncode() + "')" ) 
    .length; 
    return nresult;

}).reduce(function(a,b){ return a+b });

So the reduce function will execute a+b on the array returned by map().

You can then use your variable res as you want.

Sign up to request clarification or add additional context in comments.

7 Comments

Log - Cannot read property 'reduce'. Thanks, Tomisol, for your answer. You know how can I change that?
Which one gives you this error ? The return statement with nresult ?
No, the console log of entire page when I run the script
Oh, sorry, the second. The first return a null value
Which call gives you this 0,0,1,1,0 in your code ?
|

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.