如何比较对象数组中的对象值?

How to compare object values in an array of objects?

我正在尝试比较一组对象。每个对象都有相同的键,但每个键的值不同。我想创建一个函数来比较每个对象的相似键值对。

我只关心每个对象的质量和位置键,我想将所有对象与这两个键进行比较。

例如,如果对象数组中的第一个和第二个对象包含两个键的相同值,我想创建一个总结每个分组的对象输出数组。

解释:对象一和对象二包含 qualitylocation 的相同值。由于第三个对象没有,因此应创建一个新对象来汇总来自第一个和第二个对象的信息。该新对象应包含所有水果名称的数组和所有标签的数组。输出对象如下所示。

// 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'],
  },
  ...
];


您可以使用 Array.prototype.reducequalitylocationdata 进行分组,并过滤​​长度大于 1 的组。

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);

如果您还希望只保留独特的水果,那么您可以映射结果数组并使用 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);

其他相关文件: