使用.map()或.filter()函数对数组中的对象进行分组

时间:2019-01-03 05:35:17

标签: javascript arrays object filter

我有一个对象数组,要根据特定条件进行分组。

下面是原始数组:

let meetingOriginal = [{
    id: 19,
    datetime: '2019-01-02T13:28:03+05:30',
    status: 'proposed',
    createdBy: 'buyer',
    buyerId: 2,
    buyerFirstName: 'Demo',
    buyerLastName: 'User',
    buyerProfileUrl: null,
    petId: 1,
    petName: 'Penny',
    petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg',
    ownerFirstName: 'Faizan',
    ownerLastName: 'Saiyed',
    ownerProfileUrl: null,
    ownerId: 3,
  },
  {
    id: 20,
    datetime: '2019-01-03T13:28:06+05:30',
    status: 'proposed',
    createdBy: 'buyer',
    buyerId: 2,
    buyerFirstName: 'Demo',
    buyerLastName: 'User',
    buyerProfileUrl: null,
    petId: 1,
    petName: 'Penny',
    petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg',
    ownerFirstName: 'Faizan',
    ownerLastName: 'Saiyed',
    ownerProfileUrl: null,
    ownerId: 3,
  },
  {
    id: 21,
    datetime: '2019-01-04T13:28:09+05:30',
    status: 'proposed',
    createdBy: 'buyer',
    buyerId: 2,
    buyerFirstName: 'Demo',
    buyerLastName: 'User',
    buyerProfileUrl: null,
    petId: 1,
    petName: 'Penny',
    petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg',
    ownerFirstName: 'Faizan',
    ownerLastName: 'Saiyed',
    ownerProfileUrl: null,
    ownerId: 3,
  },
  {
    id: 22,
    datetime: '2019-01-04T13:28:09+05:30',
    status: 'proposed',
    createdBy: 'buyer',
    buyerId: 3,
    buyerFirstName: 'Demo',
    buyerLastName: 'User',
    buyerProfileUrl: null,
    petId: 2,
    petName: 'Jerry',
    petMedia: 'https://static1.squarespace.com/static/52784cdde4b07cdbb003018f/t/56259d40e4b043d43a71cc73/1445305665196/Cavapoo-dog-painting-square-a.jpg?format=500w',
    ownerFirstName: 'Jony',
    ownerLastName: 'Snowy',
    ownerProfileUrl: null,
    ownerId: 5,
  }
];

如何仅使用.map()或.filter()方法(严格来说,没有库或for&foreach函数)对这些对象进行分组,如下所示?

let requiredMeeting = [{
    buyerId: 2,
    buyerFirstName: 'Demo',
    buyerLastName: 'User',
    buyerProfileUrl: null,
    petId: 1,
    petName: 'Penny',
    petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg',
    ownerFirstName: 'Faizan',
    ownerLastName: 'Saiyed',
    ownerId: 3,
    ownerProfileUrl: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546338985171.jpg',
    meetings: [{
        id: 19,
        datetime: '2019-01-02T13:28:03+05:30',
        status: 'proposed',
        createdBy: 'buyer'
      },
      {
        id: 20,
        datetime: '2019-01-03T13:28:06+05:30',
        status: 'accepted',
        createdBy: 'buyer'
      },
      {
        id: 21,
        datetime: '2019-01-04T13:28:09+05:30',
        status: 'proposed',
        createdBy: 'buyer'
      }
    ]
  },
  {
    buyerId: 3,
    buyerFirstName: 'Demo',
    buyerLastName: 'User',
    buyerProfileUrl: null,
    petId: 2,
    petName: 'Jerry',
    petMedia: 'https://static1.squarespace.com/static/52784cdde4b07cdbb003018f/t/56259d40e4b043d43a71cc73/1445305665196/Cavapoo-dog-painting-square-a.jpg?format=500w',
    ownerFirstName: 'Jony',
    ownerLastName: 'Snowy',
    ownerId: 5,
    ownerProfileUrl: 'https://cdna.artstation.com/p/assets/images/images/002/898/132/large/hector-jenz-jonhsnow-square-detaill.jpg?1467032227',
    meetings: [{
      id: 22,
      datetime: '2019-01-04T13:28:09+05:30',
      status: 'proposed',
      createdBy: 'buyer'
    }]
  }
];

可能有多个对象以及不同的petId和ownerId。

这是小提琴: LINK

2 个答案:

答案 0 :(得分:3)

您可以使用Reduce执行此操作

let meetingOriginal =[{id:19,datetime:'2019-01-02T13:28:03+05:30',status:'proposed',createdBy:'buyer',buyerId:2,buyerFirstName:'Demo',buyerLastName:'User',buyerProfileUrl:null,petId:1,petName: 'Penny',petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg',ownerFirstName:'Faizan',ownerLastName: 'Saiyed',ownerProfileUrl: null,ownerId: 3,},{id: 20,datetime: '2019-01-03T13:28:06+05:30',status: 'proposed',   createdBy: 'buyer', buyerId: 2, buyerFirstName: 'Demo',   buyerLastName: 'User',  buyerProfileUrl: null,   petId: 1,  petName: 'Penny',  petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg',  ownerFirstName: 'Faizan',   ownerLastName: 'Saiyed', ownerProfileUrl: null,   ownerId: 3,},{ id: 21, datetime: '2019-01-04T13:28:09+05:30', status: 'proposed',  createdBy: 'buyer',  buyerId: 2,  buyerFirstName: 'Demo',  buyerLastName: 'User',  buyerProfileUrl: null,  petId: 1,  petName: 'Penny', petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg', ownerFirstName: 'Faizan',  ownerLastName: 'Saiyed', ownerProfileUrl: null,   ownerId: 3,},{ id: 22, datetime: '2019-01-04T13:28:09+05:30', status: 'proposed',   createdBy: 'buyer', buyerId: 3, buyerFirstName: 'Demo',   buyerLastName: 'User', buyerProfileUrl: null, petId: 2,   petName: 'Jerry', petMedia: 'https://static1.squarespace.com/static/52784cdde4b07cdbb003018f/t/56259d40e4b043d43a71cc73/1445305665196/Cavapoo-dog-painting-square-a.jpg?format=500w', ownerFirstName: 'Jony',  ownerLastName: 'Snowy', ownerProfileUrl: null,   ownerId: 5,}];

let op = meetingOriginal.reduce((o,c)=>{
  if(o[c.buyerId]){
    o[c.buyerId]['meetings'].push({
        id: c.id, datetime: c.dateTime, status: c.status, createdBy: c.createdBy
    })
  } else {
  let  { buyerId,buyerFirstName,buyerLastName,buyerProfileUrl,petId,petName,petMedia,ownerFirstName,ownerLastName,ownerId,ownerProfileUrl} = c
    o[c.buyerId]={buyerId: buyerId, buyerFirstName:buyerFirstName, buyerLastName:buyerLastName, buyerProfileUrl:buyerProfileUrl, petId:petId, petName:petName, petMedia:petMedia, ownerFirstName:ownerFirstName, ownerLastName:ownerLastName, ownerId:ownerId, ownerProfileUrl:ownerProfileUrl, meetings : [{ datetime: c.dateTime, status: c.status, createdBy: c.createdBy }]}}
  return o;
},{})

let finalOp = Object.values(op);
console.log(finalOp)

答案 1 :(得分:1)

我已经使用map()尝试了解决方案:

let inputObjs = [
    {
        grpId: 2,
        name: 'group_2',
        id: 1,
        date: '2019-01-02'
    },
    {
        grpId: 2,
        name: 'group_2',
        id: 2,
        date: '2019-01-03'
    },
    {
        grpId: 5,
        name: 'group_5',
        id: 1,
        date: '2019-01-01'
    }
];

var map = new Map();

inputObjs.map(obj => {

    var grp = obj.grpId;
    var mapVal = map.get(grp);

    if (! mapVal) {

        map.set(grp,
                {
                    grpId : obj.grpId, 
                    name : obj.name, 
                    meetings : [
                            {id: obj.id, date: obj.date}
                        ]
                }
            );
    }
    else {
        mapVal.meetings.push({id: obj.id, date: obj.date});
    }
});

// print the output
for (let e of map.values()) {
    console.log(e);
}

结果:

[
    {
        grpId: 2,
        name: 'group_2',
        meetings: [
                    {id: 1, date: '2019-01-02'},
                    {id: 2, date: '2019-01-03'}
                ]
    },
    {
        grpId: 5,
        name: 'group_5',
        meetings: [
                    {id: 1, date: '2019-01-01'}
        ]
    }
];