1
function doesOwnSet(player, type) {
// we'll use "chaining" here, so every next method will be called upon
// what previous method have returned

// `return` statement will return the result of the very last method

// first, lets take an array of `position` object keys
// which are "position1", "position2" and so on
return Object.keys(positions)

    // then, create an array of positions object
    // this will return Array
    .map(function (key) {
        return positions[key];
    })

    // then, pick up only positions with specified type (aka set)
    // this will return Array
    .filter(function (pos) {
        return pos.type === type;
    })

    // finally, check if specified player owns every position of the set
    // this will return Boolean
    .every(function (pos) {
        return pos.owner === player;
    });
}

I don't understand where the words "key" and "pos" come from. Are these the names of the functions? I really don't get it despite the comments. It was an answer to this question. The code works, but I just don't understand what it does.

Community
  • 1
  • 1
Elton Frederik
  • 259
  • 1
  • 3
  • 12

1 Answers1

2

Object.keys returns an Array of the "own" keys of the object.

The .map(), .filter() and .every() iterate the array, passing each value to the callback one at a time, so the key in .map() is the object key for the current iteration.

Whatever .map() callback returns gets added as a member of a new Array, which is returned, so .filter() and .every() operate on the new Array values.

The .filter() will create an Array of the values where the callback returned true (or any truthy value).

The .every() will return true if its callback returns a truthy value for every iteration, otherwise it returns false.

Put calls to console.log() inside the callbacks to see what the values hold.

function doesOwnSet(player, type) {
// we'll use "chaining" here, so every next method will be called upon
// what previous method have returned

// `return` statement will return the result of the very last method

// first, lets take an array of `position` object keys
// which are "position1", "position2" and so on
return Object.keys(positions)

    // then, create an array of positions object
    // this will return Array
    .map(function (key) {
        console.log(key);
        return positions[key];
    })

    // then, pick up only positions with specified type (aka set)
    // this will return Array
    .filter(function (pos) {
        console.log(pos);
        return pos.type === type;
    })

    // finally, check if specified player owns every position of the set
    // this will return Boolean
    .every(function (pos) {
        console.log(pos);
        return pos.owner === player;
    });
}
cookie monster
  • 10,671
  • 4
  • 31
  • 45
  • 1
    +1 for showing the use of console.log to show what this is actually doing. I also think it's worth mentioning anonymous functions/delegates - http://stackoverflow.com/questions/1140089/how-does-an-anonymous-function-in-javascript-work. In this case, _key_ and _pos_ are just parameters of the delegate. – Aaron Newton Jan 01 '14 at 21:16
  • I just did it. It basically mirrors the values of my "positions" object. – Elton Frederik Jan 01 '14 at 21:18
  • @EltonFrederik: The `key` parameter will give the keys, not the values. In other words, if `positions` is `{foo: "bar", baz: "buz"}`, you'll get `foo` and `baz`. To get the values you'd use `positions[key]`, which will be the same as `positions["foo"]` and `positions["baz"]` – cookie monster Jan 01 '14 at 21:21
  • ...the values are then mapped to a new Array because `.map()` returns `positions[key]`, so `.filter()` and `every()` will operate on the values, so `pos` would be `"bar"` and `"buz"`. – cookie monster Jan 01 '14 at 21:22
  • So pos is just a name, it could have been position or p etc ? – Elton Frederik Jan 01 '14 at 21:25
  • @EltonFrederik: Yes it's a function parameter, which is a name that references whatever was passed to the function. – cookie monster Jan 01 '14 at 21:29