3gwebtrain
3gwebtrain

Reputation: 15309

How to get result from `filter` by `return`ing the result?

I am trying to return a filtered result. But I am getting the jQuery object instead of the length.

Is there a way to do this?

var userNoteClass = spans.filter(function(index) {
    return this.className.split(' ').reduce(function(cNum, cName) {
        return cName.substring(0, 8) === 'userNote' ? cNum + 1 : cNum;
    }, 0).length; //i required the cNum to be consoled out.
});

console.log(userNoteClass); // length i am looking here..

Upvotes: 0

Views: 50

Answers (1)

Amit Joki
Amit Joki

Reputation: 59282

What you're doing is not filtering. You need Array.map.

var userNoteClass = spans.map(function(index) {
    return this.className.split(' ').reduce(function(cNum, cName) {
        return cName.substring(0, 8) === 'userNote' ? cNum + 1 : cNum;
    }, 0); // leave it as it is
});
console.log(userNoteClass);

Reduce function is returning a Number, so there's no need of length, I suppose.

Upvotes: 1

Related Questions