skip to Main Content

Let’s say I have the following object:

const myObjects = [
    { name: "Alice", age: 30 },
    { name: "Bob", age: 25 },
    { name: "Alice", age: 30 },
    { name: "David", age: 25 }
];

I wrote the following function, so that the objects with the specified same props are returned in a new array:

function findObjectsWithSameProperties(objects, property1, property2) {
  // Create an empty array to store the results
  const matchingObjects = [];

  // Loop through each object in the array
  for (const obj of objects) {
    // Check if the object has both specified properties
    if (obj.hasOwnProperty(property1) && obj.hasOwnProperty(property2)) {
      // Check if the values of the specified properties are the same
      if (obj[property1] === obj[property2]) {
        // If they are, add the object to the results array
        matchingObjects.push(obj);
      }
    }
  }

  // Return the array of matching objects
  return matchingObjects;
}

However, my function isn’t returning anything when I expect it to return this:

[
    { name: "Alice", age: 30 },
    { name: "Alice", age: 30 },
];

Here’s how I’m calling the function: findObjectsWithSameProperties(myObjects, "age", "name");

Does anyone know what I’m doing wrong? Here’s a CodePen link: https://codepen.io/obliviga/pen/WNWNbMX?editors=0011

2

Answers


  1. You’re looping through objects, checking whether the object has both props and then checking whether the name === age. Furthermore you’re checking the same object’s properties so you won’t find duplicates either. This will obviously never be true. I suggest adding a history of the ones you’ve already looped through and then comparing whether it has already seen the same object or not.

    function findObjectsWithSameProperties(objects, property1, property2) {
      // Create an empty array to store the results
      const matchingObjects = [];
      const seen = {};
      
      // Loop through each object in the array
      for (const obj of objects) {
        // Check if the object has both specified properties
        if (obj.hasOwnProperty(property1) && obj.hasOwnProperty(property2)) {
          const key = obj[property1] + '-' + obj[property2];
          // Check if we have seen this combination before
          if (seen[key]) {
            // Increment if it already exists
            seen[key].count++;
            // Add the object to the array of matching objects
            seen[key].objects.push(obj);
          } else {
            // If it's the first time it's ever seen this object, set count to 1
            seen[key] = { count: 1, objects: [obj] };
          }
        }
      }
    
      
      for (const key in seen) {
        if (seen[key].count > 1) {
          matchingObjects.push(...seen[key].objects); 
    //count essentially keeps track of how many times it's already seen that object. If there has been a duplicate, it pushes the objects your matchingObjects array.
        }
      }
    
      // Return the array of matching objects
      return matchingObjects;
    }
    
    Login or Signup to reply.
  2. try this :

    const myObjects = [
        { name: "Alice", age: 30 },
        { name: "Bob", age: 25 },
        { name: "Alice", age: 30 },
        { name: "David", age: 25 }
    ];
    
    const matchingObjects = (objectsArray,...properties) => objectsArray.filter( ( mainObject,index ) => 
        objectsArray.filter( ( obj,idx ) => idx != index ).some( object => 
            properties.every( prop => mainObject[prop] == object[prop] )));
    
    console.log(matchingObjects(myObjects,"name"));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search