Map, filter, reduce

Taken from here: https://www.youtube.com/playlist?list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84

by Rob Carroll

JavaScript

var animals = [{
    name: 'Fluffykins',
    species: 'rabbit',
    color: 'white'
}, {
    name: 'Caro',
    species: 'dog',
    color: 'black'
}, {
    name: 'Hamilton',
    species: 'dog',
    color: 'auburn'
}, {
    name: 'Harold',
    species: 'fish',
    color: 'blue'
}, {
    name: 'Ursula',
    species: 'cat',
    color: 'ginger'
}, {
    name: 'Jimmy',
    species: 'fish',
    color: 'gold'
}];

var isDog = function (animal) {
    return animal.species === 'dog';
};
var dogs = animals.filter(isDog);
var otherAnimals = _.reject(animals, isDog);
console.log(dogs);
console.log(otherAnimals);
var names = animals.map(function (animal) {
    this.animal = {};
    this.animal.name = animal.name;
    this.animal.color = animal.color;
    return this.animal;
});
console.log(names);

var orders = [{
    amount: 250
}, {
    amount: 400
}, {
    amount: 100
}, {
    amount: 325
}];

var totalAmount = orders.reduce(function (sum, order) {
    return sum + order.amount;
}, 0);
console.log(totalAmount);

var orders2 = [
    ["Mark Johansson", "waffle iron", 80, 2],
    ["Mark Johansson", "blender", 200, 1],
    ["Mark Johansson", "knife", 10, 4],
    ["Nikita Smith", "waffle iron", 80, 1],
    ["Nikita Smith", "knife", 10, 2],
    ["Nikita Smith", "pot", 20, 3]
];

var output = orders2.reduce(function (customers, line) {
    customers[line[0]] = customers[line[0]] || [];
    customers[line[0]].push({
        name: line[1],
        price: line[2],
        quantity: line[3]
    });
    return customers;
}, {});
console.log("orders2 output:", JSON.stringify(output, null, 2));