function whatIsInAName(collection, source) {
var result = [];
var arr1=Object.keys(source);
console.log(arr1);
for(var i=0;i<collection.length;i++){
for(var j=0;j<arr1.length;j++){
if(collection[i].hasOwnProperty(arr1[j])===false){ //Check 1 if false go to next object in collection
break;
}
else if(collection[i].hasOwnProperty(arr1[j])){
console.log(source[arr1[j]],collection[i][arr1[j]])
if(source[arr1[j]]!==collection[i][arr1[j]]){ //Check 2 if value is not equal break loop and goto next object in collection
break;
}
continue; // if both check passes go for next property of source to check in object;
}
result.push(collection[i]); //if all values are present and checked in object push it in result array.
}
}
return result;
}
console.log(whatIsInAName([{ “a”: 1, “b”: 2 }, { “a”: 1 }, { “a”: 1, “b”: 2, “c”: 2 }], { “a”: 1, “b”: 2 }));
Hey Guys I couldnt figure out the problem in my logic. I try to debug it even but cant find what the %* is a problem with logic. The program is to Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching property and value pairs (second argument). Kindly help me over please.
This is opinion, mind, but I find it rather clunky to compare anything to false. What is the intended action if not false?
if (obj[i].hasOwnProperty(prop) {
// do something
} else {
break;
}
Two things you could do to help yourself (and your readers)…
Use shorter variable names (collection is a bit long).
Use ample whitespace so operators and variables stand out.
Why afix a number to something when there is only one?
arr1 => arr
Quite often we see, obj to represent a collection (as you are calling it). A true collection in JavaScript is a DOM object that represents part of the document tree.
document.querySelectorAll('div');
would be such a collection.
Give the above notes some consideration and see it you cannot clean up your code so it’s easier to read and study for logic errors.
var result = [];
var arr1=Object.keys(source);
console.log(arr1);
for(var i=0;i<collection.length;i++){
for(var j=0;j<arr1.length;j++){
if(collection[i].hasOwnProperty(arr1[j])){ //check 1 if source and collection property matches
//console.log(source[arr1[j]],collection[i][arr1[j]])
if(source[arr1[j]]!==collection[i][arr1[j]]){ //Check 2 if value is not equal break loop and goto next object in collection
break;
}
continue; // if both check passes go for next property of source to check in object;
}
else { //check 1 if source and collection property not matches break the loop and goto next object in collection
break;
}
result.push(collection[i]); //if all values are present and checked in object push it in result array.
}
}
return result;
}
console.log(whatIsInAName([{ a: 1, b: 2 }, { a: 1 }, { a: 1, b: 2, c: 2 }], { a: 1, b: 2 }));
What you are doing currently is that if all the if statements pass, and loops do not break anywhere, you put continue again at the last item of arr1. So, it goes to check next iteration, does not find it, and goes to next iteration in the collection loop. While doing this, it does not push any item to result.
I still haven’t studied your code, beyond getting the gist of what the objective is. Rather my choice was to look at the problem fresh and hammer out a solution. That gives you something to compare with that may or may not coincide with your own logic.
Here is a functional approach to the object in an array problem…
const findObjectInArray = (haystack, needle) => {
let needleSize = Object.keys(needle).length;
let x;
for (let i = 0; i < haystack.length; i++) {
let obj = haystack[i];
if (Object.keys(obj).length < needleSize) continue;
for (let key in obj) {
if (needle.hasOwnProperty(key) && obj[key] === needle[key]) {
x = i;
} else {
x = false;
break;
}
}
if (x) break;
}
return x;
};
let s = { a: 1, b: 2 };
let c = [{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1, b: 2, c: 2 }, { a: 1, b: 2 }];
console.log(result=findObjectInArray(c, s), c[result]);
//-> 4 { a: 1, b: 2 }