简体   繁体   English

通过多个键减少对象数组并将它们推入子集数组

[英]Reduce an array of objects by multiple keys and push them into a subset array

I have the following object:我有以下 object:

const array = [
    {city: 'Auckland', country: 'New Zealand', date: '2024-02-03T00:00:00'},
    {city: 'Manchester', country: 'United Kingdom', date: '2024-02-04T00:00:00'},
    {city: 'Manchester', country: 'United Kingdom', date: '2024-02-09T00:00:00'},
    {city: 'Edinburgh', country: 'Scotland', date: '2024-02-05T00:00:00'},
    {city: 'Manchester', country: 'United States', date: '2024-02-03T00:00:00'},
    {city: 'Manchester', country: 'United States', date: '2024-02-09T00:00:00'}
]

I want it grouped like the following:我希望它像下面这样分组:

{
    city: 'Auckland',
    items: [
        {city: 'Auckland', country: 'New Zealand', date: '2024-02-03T00:00:00'}
    ]
},
{
    city: 'Manchester',
    items: [
        {city: 'Manchester', country: 'United Kingdom', date: '2024-02-04T00:00:00'},
        {city: 'Manchester', country: 'United Kingdom', date: '2024-02-09T00:00:00'}
  ]
},
{
    city: 'Edinburgh',
    items: [
        {city: 'Edinburgh', country: 'Scotland', date: '2024-02-05T00:00:00'}
    ]
},
{
    city: 'Manchester',
    item: [
        {city: 'Manchester', country: 'United States', date: '2024-02-03T00:00:00'},
        {city: 'Manchester', country: 'United States', date: '2024-02-09T00:00:00'}
    ]
}

I have tried multiple different methods.我尝试了多种不同的方法。 I scoured the.net for a similar thread, especially on here but have not found a similar one.我在 .net 上搜索了类似的线程,尤其是在此处,但没有找到类似的线程。 I've tried the following:我试过以下方法:

array.reduce((r, item) => {
    r[item.city] = r[item.city] || []
    r[item.city].push(item)

    return r
}, Object.create(null))

However, this obviously does not take "country" into account.但是,这显然没有考虑“国家”。 I've also tried the following but that only returns the first item in the desired group:我也尝试了以下但只返回所需组中的第一项:

    const cities = {};

    for (let p of array) {
      const { city, country } = p;
      const groupByCity = JSON.stringify([city]);
      const groupByCountry = JSON.stringify([country]);
      if (!(groupByCity in cities)) {
        cities[groupByCity] = { city: city, items: []};
        cities[groupByCity].items.push(p);
      } else if (groupByCity in examCities && cities[groupByCity].country !== p.country) {
        cities[`${groupByCity}2`] = { city: city, items: []};
        cities[`${groupByCity}2`].items.push(p);
      }
    }

There are many ways to do this, here's my way by using reduce and map:有很多方法可以做到这一点,这是我使用 reduce 和 map 的方法:

 const array = [{ city: 'Auckland', country: 'New Zealand', date: '2024-02-03T00:00:00' }, { city: 'Manchester', country: 'United Kingdom', date: '2024-02-04T00:00:00' }, { city: 'Manchester', country: 'United Kingdom', date: '2024-02-09T00:00:00' }, { city: 'Edinburgh', country: 'Scotland', date: '2024-02-05T00:00:00' }, { city: 'Manchester', country: 'United States', date: '2024-02-03T00:00:00' }, { city: 'Manchester', country: 'United States', date: '2024-02-09T00:00:00' } ] const result = Object.entries(array.reduce((acc, curr) => { const key = `${curr.city}|${curr.country}` return {...acc, [key]: key in acc? [...acc[key], curr]: [curr] } }, {})).map(([key, value]) => ({ city: value[0].city, items: value })) console.log(result)

Basically I group them by {city}|{country} and form an object with reduce , then transform that object to the desired array with map .基本上,我按{city}|{country}对它们进行分组,并使用reduce形成一个 object ,然后使用map将该 object 转换为所需的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM