1

I am trying to compare an array of objects. Each object has the same keys, but varying values for each key. I would like to create a function that compares each object for similar key value pairs.

I only care about the quality and location keys for each object, and I want to compare all objects against these two keys.

For example, if the first and second object in the array of objects contains the same value for two keys, I would like to create an output array of objects that summarizes each grouping.

Explanation: Object one and two contain the same value for quality and location. Since the third object does not, a new object should be created that summarizes the information from the first and second objects. That new object should contains an array of all fruit names and and array of all tags. The output object is shown below.

// Input
data = [
  {
    id: '1',
    fruit: 'granny smith',
    quality: 'good',
    location: 'chicago',
    tags: ['green', 'sweet'],
  },
  {
    id: '2',
    fruit: 'Fuji',
    quality: 'good',
    location: 'chicago',
    tags: ['red', 'tart'],
  },
  {
    id: '3',
    fruit: 'gala',
    quality: 'bad',
    location: 'san diego',
    tags: ['tall', 'thin'],
  },
];

// Function
function compareObjects(arr) {
  const grouped = [];

  // Loop over every fruit
  const similarObjects = arr.filter((obj, id) => {
    // create structure for each common object
    let shape = {
      id,
      fruits: [],
      quality: '',
      tags: [],
    };

    arr.forEach((item) => {
      // Return a new shape object that contains all fruit names, tags, and quality
      if (item.quality == obj.quality && item.location == obj.location) {
        shape.id = id;
        shape.fruits.push(item.fruit);
        shape.fruits.push(obj.fruit);
        shape.quality = item.quality;
        shape.tags.push(item.tag);
        shape.tags.push(obj.tag);
        return shape;
      }
    });
    return obj;
  });

  return similarObjects;
}

console.log(compareObjects(data));

// Output
const output = [
  {
    id: 'a',
    fruits: ['grann smith', 'fuji'],
    quality: 'good',
    tags: ['green', 'sweet', 'red', 'tart'],
  },
  ...
];


2
  • Does the third object appear in the output? And where did id "a" come from? Commented May 26, 2022 at 18:56
  • 1
    The third object should not appear in the output because it does not match any key value pairs of the other objects. "a" is just a uniquely generated id. It doesn't serve much purpose in this. Commented May 26, 2022 at 19:00

1 Answer 1

1

You can group the data by their quality and location using Array.prototype.reduce and filter the groups where the length is greater than one.

const 
  data = [
    { id: "1", fruit: "granny smith", quality: "good", location: "chicago", tags: ["green", "sweet"] },
    { id: "2", fruit: "Fuji", quality: "good", location: "chicago", tags: ["red", "tart"] },
    { id: "3", fruit: "gala", quality: "bad", location: "san diego", tags: ["tall", "thin"] },
  ],
  output = Object.values(
    data.reduce((r, d) => {
      const key = `${d.quality}+${d.location}`;
      if (!r[key]) {
        r[key] = { id: d.id, fruits: [], quality: d.quality, location: d.location, tags: [] };
      }
      r[key].fruits.push(d.fruit);
      r[key].tags.push(...d.tags);
      return r;
    }, {})
  ).filter((d) => d.fruits.length > 1);

console.log(output);

If you also wish to only keep unique fruits then you can map over the resultant array and remove the duplicates using a Set.

const 
  data = [
    { id: "1", fruit: "granny smith", quality: "good", location: "chicago", tags: ["green", "sweet"] },
    { id: "2", fruit: "Fuji", quality: "good", location: "chicago", tags: ["red", "tart"] },
    { id: "3", fruit: "gala", quality: "bad", location: "san diego", tags: ["tall", "thin"] },
  ],
  output = Object.values(
    data.reduce((r, d) => {
      const key = `${d.quality}+${d.location}`;
      if (!r[key]) {
        r[key] = { id: d.id, fruits: [], quality: d.quality, location: d.location, tags: [] };
      }
      r[key].fruits.push(d.fruit);
      r[key].tags.push(...d.tags);
      return r;
    }, {})
  )
    .filter((d) => d.fruits.length > 1)
    .map((d) => ({ ...d, fruits: [...new Set(d.fruits)] }));

console.log(output);

Other relevant documentations:

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

6 Comments

This looks good. Can you combine the tags from both objects in the output? The tags output should look like this: tags: [ 'red', 'tart', 'green', 'sweet' ]
Oh yeah, fixed!
I see what you did there. You just spread the two arrays. Great!
One more quick thing. I am trying to remove duplicate fruit names in the fruit names array. For example, if both objects had the fruit name: 'granny smith', the output fruits array should only contain 'granny smith', once, not twice.
@TylerMorales That's correct, you can also do it all at once at the very end by chaining a .map call, I've updated my answer.
|

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.