根据值将数组收集到子数组存储桶中(数组本身就是)

索拉·库玛(Saurabh Kumar)

我有一个如下的Javascript对象数组。

[
  {
    email: '[email protected]',
    fn: 'Alex',
    sn: 'McPherson',
    phone: '01233xxxxx',
    hours: '40',
    rate: '20',
    amount: '200',
    vat: '60',
    agency: 'test',
    start: '08/06/2017',
    end: '10/06/2017',
    shipping: {
      addresses: [
        {
          id: '1234',
          area: 'xzy'
        },
        {
          id: '2345',
          area: 'uhj'
        }
      ]
    }
  },
  {
    email: '[email protected]',
    fn: 'Mike',
    sn: 'Mann',
    phone: '01233xxxxx',
    hours: '50',
    rate: '70',
    amount: '500',
    vat: '90',
    agency: 'test',
    start: '08/06/2017',
    end: '10/06/2017',
    shipping: {
      addresses: [
        {
          id: '1234',
          area: 'xzy'
        },
        {
          id: '3456',
          area: 'uio'
        }
      ]
    }
  },
  {
    email: '[email protected]',
    fn: 'Fred',
    sn: 'Frogg',
    phone: '01233xxxxx',
    hours: '80',
    rate: '90',
    amount: '800',
    vat: '100',
    agency: 'test',
    start: '08/06/2017',
    end: '10/06/2017',
    shipping: {
      addresses: [
        {
          id: '4567',
          area: 'asdaf'
        },
        {
          id: '3456',
          area: 'uio'
        }
      ]
    }
  },
  {
    email: '[email protected]',
    fn: 'Alex',
    sn: 'McPherson',
    phone: '01233xxxxx',
    hours: '90',
    rate: '30',
    amount: '900',
    vat: '120',
    agency: 'test',
    start: '08/06/2017',
    end: '10/06/2017',
    shipping: {
      addresses: [
        {
          id: '4567',
          area: 'asdaf'
        },
        {
          id: '5678',
          area: 'asdf'
        }
      ]
    }
  }
]

我理想的情况是将那些具有相同值(shipping.addresses.id)的对象分组到自己的对象子数组中。预期结果。

[
  {
    id: '1234',
    area: 'xzy',
    data: [
      {
        email: '[email protected]',
        fn: 'Alex',
        sn: 'McPherson',
        phone: '01233xxxxx',
        hours: '40',
        rate: '20',
        amount: '200',
        vat: '60',
        agency: 'test',
        start: '08/06/2017',
        end: '10/06/2017',
        shipping: {
          addresses: [
            {
              id: '1234',
              area: 'xzy'
            },
            {
              id: '2345',
              area: 'uhj'
            }
          ]
        }
      },
      {
        email: '[email protected]',
        fn: 'Mike',
        sn: 'Mann',
        phone: '01233xxxxx',
        hours: '50',
        rate: '70',
        amount: '500',
        vat: '90',
        agency: 'test',
        start: '08/06/2017',
        end: '10/06/2017',
        shipping: {
          addresses: [
            {
              id: '1234',
              area: 'xzy'
            },
            {
              id: '3456',
              area: 'uhj'
            }
          ]
        }
      }
    ]
  },
  {
    id: '2345',
    area: 'uhj',
    data: [
      {
        email: '[email protected]',
        fn: 'Alex',
        sn: 'McPherson',
        phone: '01233xxxxx',
        hours: '40',
        rate: '20',
        amount: '200',
        vat: '60',
        agency: 'test',
        start: '08/06/2017',
        end: '10/06/2017',
        shipping: {
          addresses: [
            {
              id: '1234',
              area: 'xzy'
            },
            {
              id: '2345',
              area: 'uio'
            }
          ]
        }
      }
    ]
  },
  {
    id: '3456',
    area: 'uio',
    data: [
      {
        email: '[email protected]',
        fn: 'Mike',
        sn: 'Mann',
        phone: '01233xxxxx',
        hours: '50',
        rate: '70',
        amount: '500',
        vat: '90',
        agency: 'test',
        start: '08/06/2017',
        end: '10/06/2017',
        shipping: {
          addresses: [
            {
              id: '1234',
              area: 'xzy'
            },
            {
              id: '3456',
              area: 'uio'
            }
          ]
        }
      },
      {
        email: '[email protected]',
        fn: 'Fred',
        sn: 'Frogg',
        phone: '01233xxxxx',
        hours: '80',
        rate: '90',
        amount: '800',
        vat: '100',
        agency: 'test',
        start: '08/06/2017',
        end: '10/06/2017',
        shipping: {
          addresses: [
            {
              id: '4567',
              area: 'asdaf'
            },
            {
              id: '3456',
              area: 'uio'
            }
          ]
        }
      }
    ]
  }
]

我可以使用特定的键(使用下面的代码)使用特定的属性来对输入数组进行分组,但是我似乎无法基于基于键(本身就是数组)来求助于该数组。

Array.from(
    data.reduce( 
        (acc, o) => (acc.get(o.email).push(o), acc),
        new Map(data.map( o => [o.email, []] ))
    ), ([key, value]) => value
)
沙丁鱼

您可以data使用shipping.addresses.id数组简化为对象,并使用来返回数组Object.values()您将需要遍历addresses每个对象数组,并id在遇到每个对象时为其创建一个条目,并为具有相同元素的后续元素推送到这些条目id

const byAddressId = Object.values(
  data.reduce((a, o) => {
    o.shipping.addresses.forEach(({id, area}) => {
      a[id]  = {...a[id] ?? {id: id, area: area, data: []}};
      a[id]['data'].push({...o});
    });
    return a;  
  }, {}));

const data = [{"email": "[email protected]","fn": "Alex","sn": "McPherson","phone": "01233xxxxx","hours": "40","rate": "20","amount": "200","vat": "60","agency": "test","start": "08/06/2017","end": "10/06/2017","shipping": {   "addresses": [  { "id": "1234", "area": "xzy"  },  { "id": "2345", "area": "uhj"  }   ]}},{"email": "[email protected]","fn": "Mike","sn": "Mann","phone": "01233xxxxx","hours": "50","rate": "70","amount": "500","vat": "90","agency": "test","start": "08/06/2017","end": "10/06/2017","shipping": {   "addresses": [  { "id": "1234", "area": "xzy"  },  { "id": "3456", "area": "uio"  }   ]}},{"email": "[email protected]","fn": "Fred","sn": "Frogg","phone": "01233xxxxx","hours": "80","rate": "90","amount": "800","vat": "100","agency": "test","start": "08/06/2017","end": "10/06/2017","shipping": {   "addresses": [  { "id": "4567", "area": "asdaf"  },  { "id": "3456", "area": "uio"  }   ]}},{"email": "[email protected]","fn": "Alex","sn": "McPherson","phone": "01233xxxxx","hours": "90","rate": "30","amount": "900","vat": "120","agency": "test","start": "08/06/2017","end": "10/06/2017","shipping": {   "addresses": [  { "id": "4567", "area": "asdaf"  },  { "id": "5678", "area": "asdf"  } ]}}];

// return array of Object.values from the accumulator
const byAddressId = Object.values(
  // reduce the data array into an object with shipping.addresses.id as keys
  data.reduce((a, o) => {
    // iterate over all addresses for each element
    o.shipping.addresses.forEach(({id, area}) => {
      // check if an id entry exists, otherwise create one
      a[id]  = {...a[id] ?? {id: id, area: area, data: []}};
      // push the object to the data array of the id object
      a[id]['data'].push({...o});
    });
    return a;  
  }, {}));

console.log(byAddressId);

话虽如此,与您在问题中包含map()group by email示例相比,您可以使用相同的方法来节省两个调用

const byEmail = Object.values(
    data.reduce((a, o) => (a[o.email] = [...a[o.email] ?? [], {...o}], a), {}));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将输入收集到数组中

如何将多个值收集到数组中,然后在IN子句中使用数组

Laravel收集到数组

角度可观察对象以及如何遍历对象数组以将字段收集到数组中

AQL如何将文件以其收集名称收集到数组中?

如何将值收集到数组中并将集合传递给C#中的datagridview?

如何从注释中将结果收集到数组中?

将记录收集到 Eve/mongodb 中的单个数组中以减少带宽

Ruby:从嵌套哈希中将目标键的值收集到数组中

使用jq将递归下降结果收集到单个数组中

PHP或Laravel Helper函数将变量收集到数组中

Java流将数组收集到一个列表中

将文档字段从 Scala 中的 Mongo DB 收集到数组

Angular observable,如何遍历具有嵌套数组的对象以将字段收集到数组中

如何在Java 8中将流的结果收集到自定义对象的数组中

如何将表中第一列的所有数据收集到数组中?

如何在Swift 5中从自定义对象将所有元素收集到数组

AngularJS-将多个get请求收集到json数组中,然后传递给指令

C将“字符串”的2D数组收集到数组中,然后将每个2D传递给C98函数

Laravel - 如何将多个数组收集到一个

尝试通过Files.walk将文件收集到数组时出现ArrayStoreException

检查数组是否包含与值匹配的数组,然后将结果存储到新数组中

摆动:将列表中的选定值存储到数组中。

如何将方法中的值存储到数组中?

将文件中的值存储到数组中

将char *值存储到无符号char **数组中

Java将文件值存储到数组中

如何使用php将json值存储到数组中?

将每个解析的JSON值存储到数组中