Skip to content

Instantly share code, notes, and snippets.

@aldraco
Created February 21, 2015 17:56
Show Gist options
  • Save aldraco/9f67eeec71fa81971b0e to your computer and use it in GitHub Desktop.
Save aldraco/9f67eeec71fa81971b0e to your computer and use it in GitHub Desktop.
Where Art Thou bonfire challenge from Free Code Camp - two versions
function where(collection, source) {
var arr = [];
var keyName = Object.keys(source)[0];
collection.forEach(function(person) {
if (person.hasOwnProperty(keyName) && (person[keyName] === source[keyName])) {
//console.log(person[keyName]);
arr.push(person);
}
});
console.log("1"+ JSON.stringify(arr));
return arr;
}
//where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' });
function whereTo(collection, source) {
var arr = [];
var keyName =Object.keys(source)[0];
collection.map(function(person){
if(person.hasOwnProperty(keyName) && (person[keyName] === source[keyName])) {
//console.log(person[keyName]);
arr.push(person);
}
});
console.log("2"+ JSON.stringify(arr));
return arr;
}
whereTo([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Montague' });
@vancovver
Copy link

screen shot 2015-08-04 at 5 36 44 pm

@denisnurboja
Copy link

function where(collection, source) {
  var arr = [];
  for(var i=0; i<collection.length;i++){
    if (collection[i].hasOwnProperty(Object.keys(source)))
        var prop = Object.keys(source);
        if (collection[i][prop]===source[prop]){
        arr.push(collection[i]);}
  }
  return arr;
}

@jfleitejr
Copy link

function where(collection, source) {
  var arr = [];
  var prop = Object.keys(source);

  for(var key in collection){
    if(collection[key].hasOwnProperty(Object.keys(source))) {
      if (collection[key][prop] === source[prop]) {
        arr.push(collection[key]);
      }
    }
  }
  return arr;
}

@bimbo1991
Copy link

is all this codes are correct !!

@Arnavk05
Copy link

Arnavk05 commented Sep 5, 2015

its not working

@gabrielgomez522
Copy link

function where(collection, source) {

var result = [];
var findKeys = Object.keys(source);

for (var i = 0; i < collection.length; i++) {

var hasProperties = true;
var currentObject = collection[i];

for (var j = 0; j < findKeys.length; j++) {
  var key = findKeys[j];

  if (!currentObject.hasOwnProperty(key) || currentObject[key] !== source[key]) {
    hasProperties = false;
  }
}
if (hasProperties) {
  result.push(currentObject);
} 

}
return result;
}

@chemok78
Copy link

here is my code:

`function where(collection, source) {

var check = true;

var arr = [];

for(i=0; i < collection.length; i++){ 

for (var prop in source){ 

    if(collection[i].hasOwnProperty(prop) && collection[i][prop] == source[prop]){ 

        check = true; 

    } else {

        check = false; 
        break;
    }
}  
if (check === true){ 

arr.push(collection[i]);   
}

}
return arr;
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment