I'm making a function that looks thru an array of the first argument and returns an array with all the objects that are matching the second argument. i dont know what is wrong with my code or how to fix it. i would appreciated it if you can help me
function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
for (let i=0; i<collection.length; i++) {
if (source.hasOwnProperty(collection[i])) {
arr.push(collection[i])
}
}
// Only change code above this line
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
I'm assuming you only want to match the last field in a object.
function whatIsInAName(collection, source) {
return collection.filter(({ last }) => last === source.last);
}
const result = whatIsInAName(
[
{ first: "Romeo", last: "Montague" },
{ first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" },
],
{ last: "Capulet" }
);
console.log(result);
Here we're just using a Filter Method on the collection array. The function predicate (({ last }) => last === source.last) we're passing in the filter will receive each object from the collection and check if source.last === received.last.
The is the same as the following one:
function doesMatch(receivedObject) {
if(receivedObject.last === source.last) return true;
else return false;
}
This is why source.hasOwnProperty isn't working as expected:
If you run console.log you will see that you are looping through the elements in collection which are objects.
Example:
function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
for (let i=0; i<collection.length; i++) {
console.log(collection[i]);
if (source.hasOwnProperty(collection[i])) {
arr.push(collection[i])
}
}
// Only change code above this line
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
But what you actually want to do (if you're checking properties, aka keys) is loop through the keys in each object.
For that, you need a second loop inside the first.
object elementskeys in each object elementUsefully, you can use:
Object.keys()
to return an array of object keys (for each object) to loop through.
So your second loop can be another for loop.
Example:
function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
for (let i=0; i<collection.length; i++) {
// Object Keys to Loop Through
let myKeys = Object.keys(collection[i]);
// Object Values
let myValues = Object.values(collection[i]);
for (let j = 0; j < myKeys.length; j++) {
if ((source.hasOwnProperty(myKeys[j])) && (source[myKeys[j]] === myValues[j])) {
arr.push(collection[i])
}
}
}
console.log(arr);
// Only change code above this line
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
This is the mistake:
if (source.hasOwnProperty(collection[i])) {
// ^ collection[i] is an object, not a property
It may be an idea to use Array.filter here. Something like:
const whatIsInAName = (collection, source) => {
// compare for all keys keys in [source]
const compare = (obj1, obj2, keys) =>
keys.filter( k => obj1[k] === obj2[k] ).length > 0;
// filter using comparison method
return collection.filter( r => compare(source, r, Object.keys(source)) );
};
const collection2CompareTo = [
{ first: "Romeo", last: "Montague" },
{ first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" }];
// expect: [{ first: "Tybalt", last: "Capulet" }],
const c1 = whatIsInAName(collection2CompareTo, { last: "Capulet" });
// expect: [{ first: "Romeo", last: "Montague" }]
const c2 = whatIsInAName(collection2CompareTo, { first: "Romeo" });
// expect: [{ first: "Romeo", last: "Montague" }]
const c3 = whatIsInAName(collection2CompareTo, { first: "Romeo", last: "Montague" });
// expect: []
const c4 = whatIsInAName(collection2CompareTo, { first: undefined, last: "Void" });
document.querySelector(`pre`).textContent = `c1: ${
JSON.stringify(c1, null, 2)}\nc2: ${
JSON.stringify(c2, null, 2)}\nc3: ${
JSON.stringify(c3, null, 2)}\nc4: ${
JSON.stringify(c4, null, 2)}`;
<pre></pre>