0

I have function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.

function whatIsInAName(collection, source) {
  let keyArr = Object.keys(source);
  for (let i = 0; i < keyArr.length; i++) {
    var arr = collection.filter(function(item) {
      return item.hasOwnProperty(keyArr[i])
    })
  }

  return arr.filter(function(item) {
    for (let g = 0; g < keyArr.length; g++) {
      return item[keyArr[g]] === source[keyArr[g]];
    }
  });

}
console.log(whatIsInAName([{
  "a": 1,
  "b": 2,
  "c": 3
}], {
  "a": 1,
  "b": 9999,
  "c": 3
}));

I should give an empty array [ ].
but It is giving [{"a": 1, "b": 2, "c": 3}]

2 Answers 2

1

You had an error in your second filter.
In for statement you were returning comparision result of first item from that statement and not checking if whole arr is equal.

In code below I have changed for statement to return false when items doesn't match. Then at the end there is returned true becouse there wasn't found any item that doesn't match with the other arr.

function whatIsInAName(collection, source) {
    let keyArr = Object.keys(source);
    
    for(let i = 0; i < keyArr.length; i++){
        var arr = collection.filter(item => item.hasOwnProperty(keyArr[i]));
    }

    return arr.filter(item => {
        for(let g = 0; g < keyArr.length; g++) {
            if (item[keyArr[g]] !== source[keyArr[g]]) {
                return false;
            }
        }
        return true;
    });
}
console.log(
    whatIsInAName(
        [{"a": 1, "b": 2, "c": 3}, {"a": 1, "b": 9999, "c": 3}],
        {"a": 1, "b": 9999, "c": 3}
    )
);

Sign up to request clarification or add additional context in comments.

Comments

0

What is the first loop for? The arr created there is overwritten in every iteration of the loop. So the final arr will be the array of items which at least contain the last property in Object.keys(). You can apply your filter directly on the collections parameter.

function whatIsInAName(collection, source) {
  let keys = Object.keys(source);
  return collection.filter(item => keys.every(k => item[k] === source[k]));
}

console.log(whatIsInAName([
    {"a": 1, "b": 9999, "c": 3 }, 
    {"a": 2, "b": 9999, "c": 3 }, 
    {"a": 1, "b": 2, "c": 3 }, 
    {"a": 1, "b": 9999, "c": 3, "d": 4 }
   ], 
   {"a": 1, "b": 9999, "c": 3}));

Keep in mind that the equality check === will only work for primitive values but not for objects or arrays. Also this approach will accept additional properties in the items of the collection, that are not present in the source item.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.