Quick actions

cmd+k|ctrl+k

Navigation

Languages

MapReduceFilter

Snippet info

Language

JavaScript

Visibility

public

Author

horngyih

Created

2017-11-21T10:18:10Z

Updated

2017-11-21T10:18:10Z

function Person( n, a ){
    this.name = n;
    this.age = a;
}

var persons = [];
persons.push( new Person( "Ace", 1 ) );
persons.push( new Person( "Beta", 2 ) );
persons.push( new Person( "Zeta", 5 ) );
persons.push( new Person( "Eta", 9) );
persons.push( new Person( "Delta", 10 ) );
persons.push( new Person( "Theta", 0 ) );

console.log( persons );

console.log( persons.filter( filterAge ) );
console.log( persons.filter( filterName ) );
console.log( persons.map( names ) );
console.log( persons.map( ages ) );
console.log( persons.filter( function(target){ return target.age > 5 }).map( ages ).reduce( sum, 0 ) );

function filterAge( target ){
    return target.age > 1;
}

function filterName( target ){
    return target.name.indexOf( 0 ) === "A";
}

function names( target ){
    if( target ){
        return target.name;
    } else {
        return null;
    }
}

function ages( target ){
    if( target ){
        return target.age;
    } else {
        return null;
    }
}

function sum( accumulator, item ){
    return accumulator + item;
}
INFO