Javascript 基于唯一值筛选对象数组

Javascript 基于唯一值筛选对象数组,javascript,arrays,hashmap,Javascript,Arrays,Hashmap,我有一个需要过滤的对象数组。 对象结构如下所示: [ { name: 'Wheat', group: { name: 'Grains' } }, { name: 'Rice', group: { name: 'Grains' } }, { name: 'corn', group: { name: 'Grains' } }, { name: 'Oats',

我有一个需要过滤的对象数组。 对象结构如下所示:

[
    {
        name: 'Wheat',
        group: { name: 'Grains' }
    }, {
        name: 'Rice',
        group: { name: 'Grains' }
    }, {
        name: 'corn',
        group: { name: 'Grains' }
    }, {
        name: 'Oats',
        group: { name: 'Grains' }
    }, {
        name: 'Live Cattle',
        group: { name: 'Livestock/meat products' }
    }, {
        name: 'pork Bellies',
        group: { name: 'Livestock/meat products' }
    }
]
{
    'Grains':                  'Oats',
    'Grains':                  'Wheat',
    'Grains':                  'corn',
    'Livestock/meat products': 'Live Cattle',
    'Livestock/meat products': 'pork Bellies'
}
我需要有这样的东西才能在GUI上显示它。 根据组名分离出数组。因为组名将是唯一的,但每个组下都可以有多个选择

所以我想创建一个HashMap,其中key作为组名,Value作为组名。 HashMap如下所示:

[
    {
        name: 'Wheat',
        group: { name: 'Grains' }
    }, {
        name: 'Rice',
        group: { name: 'Grains' }
    }, {
        name: 'corn',
        group: { name: 'Grains' }
    }, {
        name: 'Oats',
        group: { name: 'Grains' }
    }, {
        name: 'Live Cattle',
        group: { name: 'Livestock/meat products' }
    }, {
        name: 'pork Bellies',
        group: { name: 'Livestock/meat products' }
    }
]
{
    'Grains':                  'Oats',
    'Grains':                  'Wheat',
    'Grains':                  'corn',
    'Livestock/meat products': 'Live Cattle',
    'Livestock/meat products': 'pork Bellies'
}
如何使用数组函数实现这一点,或者我需要一个单独的逻辑来创建HashMap


谢谢,

您无法生成具有重复键的地图。因此,您可以替换键和值,然后就可以了,例如:

const
  data = [
      {
          name: 'Wheat',
          group: { name: 'Grains' }
      }, {
          name: 'Rice',
          group: { name: 'Grains' }
      }, {
          name: 'corn',
          group: { name: 'Grains' }
      }, {
          name: 'Oats',
          group: { name: 'Grains' }
      }, {
          name: 'Live Cattle',
          group: { name: 'Livestock/meat products' }
      }, {
          name: 'pork Bellies',
          group: { name: 'Livestock/meat products' }
      }
  ],
  result = data.map(({ name, group }) => [name, group.name]),
  resultAsMap = new Map(result);
或者,如果有帮助,可以创建一组字符串,例如:

  result = new Set(data.map(({ name, group }) => `${group.name}: ${name}`));
您还可以执行“分组方式”,这似乎更适合您的情况:

  reducer = (map, { name, group }) => map.set(group.name, (map.get(group.name) || []).concat(name)),
  result = data.reduce(reducer, new Map);

这是最好的方法。调用此函数时传递数据

function formatData(data) {
  const formattedData = {}
  data.forEach(({name, group}) => {
    formattedData[group.name] = formattedData[group.name] ? formattedData[group.name].concat(name) : [name];
  });
  return formattedData;
}

哈希映射只能有一个同名键。原始结构不是有效的对象。你不能有
组:name:“Grains”
,你需要在每个键后面有一个值。请用真正的JavaScript对象结构更新你的帖子。您显示的JS语法无效。StackOverflow不是免费的编码服务。你应该会的。请更新您的问题,以显示您已在某个应用程序中尝试过的内容。欲了解更多信息,请参阅,并选择:)为什么
Grains:Rice
不符合预期结果?删除它的筛选条件是什么?