Code expects, in one case, for you to only be pulling the key-value pair and treating it as a new individual object, and in the second case, for you to pull the full object that possesses the key-value pair.
function where(collection, source) {
var arr = [];
for(var key in source) {
for(i = 0; i < collection.length; ++i) {
if(source[key] === collection[i][key])
arr.push(collection[i]);
}
}
return arr;
}
where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' });
And output / tests:
Fails
assert.deepEqual(where([{ 'a': 1 }, { 'a': 1 }, { 'a': 1, 'b': 2 }], { 'a': 1 }), [{ 'a': 1 }, { 'a': 1 }, { 'a': 1 }], 'should return with multiples');AssertionError:should return with multiples: expected [ { a: 1 }, { a: 1 }, { a: 1, b: 2 } ] to deeply equal [ { a: 1 }, { a: 1 }, { a: 1 } ]
But
Succeeds
assert.deepEqual(where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' }), [{ first: 'Tybalt', last: 'Capulet' }], 'should return an array of objects');