JavaScript提取对象数组的匹配ID

时间:2018-06-28 11:55:23

标签: javascript arrays node.js object response

我有一个包含 employeeProjectId 的对象数组,它们可能是相同的。因此,我要寻找的是,如果有多个相同的employeeProjectId,则将结果合并为具有数组形式的 projectRoles 的一个对象。 下面给出响应以供参考。 谢谢:)

{
"code": 200,
"success": true,
"message": "Successfully completed",
"data": [
    {
        "employeeProjectId": 1,
        "projectRoleId": 1,
        "employee_project": {
            "id": 1,
            "status": true,
            "type": "backup",
            "startDate": "2018-06-28T07:20:36.000Z",
            "endDate": "2018-06-29T07:20:39.000Z",
            "allocation": 100,
            "employeeId": 326,
            "projectId": 35,
            "employee": {
                "firstName": "Asad",
                "lastName": "Marfani"
            },
            "project": {
                "name": "RTA"
            }
        },
        "project_role": {
            "id": 1,
            "role": "front_end_ios"
        }
    },
    {
        "employeeProjectId": 1,
        "projectRoleId": 2,
        "employee_project": {
            "id": 1,
            "status": true,
            "type": "backup",
            "startDate": "2018-06-28T07:20:36.000Z",
            "endDate": "2018-06-29T07:20:39.000Z",
            "allocation": 100,
            "employeeId": 326,
            "projectId": 35,
            "employee": {
                "firstName": "Asad",
                "lastName": "Marfani"
            },
            "project": {
                "name": "RTA"
            }
        },
        "project_role": {
            "id": 2,
            "role": "Front End - Android"
        }
    },
    {
        "employeeProjectId": 3,
        "projectRoleId": 1,
        "employee_project": {
            "id": 3,
            "status": true,
            "type": "backup",
            "startDate": "2018-06-28T07:47:19.000Z",
            "endDate": "2018-06-29T07:47:22.000Z",
            "allocation": 50,
            "employeeId": 16,
            "projectId": 35,
            "employee": {
                "firstName": "Nosheen",
                "lastName": "Sikandar"
            },
            "project": {
                "name": "RTA"
            }
        },
        "project_role": {
            "id": 1,
            "role": "front_end_ios"
        }
    },
    {
        "employeeProjectId": 3,
        "projectRoleId": 3,
        "employee_project": {
            "id": 3,
            "status": true,
            "type": "backup",
            "startDate": "2018-06-28T07:47:19.000Z",
            "endDate": "2018-06-29T07:47:22.000Z",
            "allocation": 50,
            "employeeId": 16,
            "projectId": 35,
            "employee": {
                "firstName": "Nosheen",
                "lastName": "Sikandar"
            },
            "project": {
                "name": "RTA"
            }
        },
        "project_role": {
            "id": 3,
            "role": "Front End - Web"
        }
    }
]}

我想要这样:

{
"code": 200,
"success": true,
"message": "Successfully completed",
"data": [
    {
        "employeeProjectId": 1,
        "employee_project": {
            "id": 1,
            "status": true,
            "type": "backup",
            "startDate": "2018-06-28T07:20:36.000Z",
            "endDate": "2018-06-29T07:20:39.000Z",
            "allocation": 100,
            "employeeId": 326,
            "projectId": 35,
            "employee": {
                "firstName": "Asad",
                "lastName": "Marfani"
            },
            "project": {
                "name": "RTA"
            }
        },
        "project_role": [
            {
                "id": 1,
                "role": "front_end_ios",
             },
             {
                "id": 2,
                "role": "front_end_web"
             }
        ]
    }
]}

2 个答案:

答案 0 :(得分:1)

您可以使用map创建一个组数组(employeeProjectIds),并使用该数组将其简化为组。

我觉得employee_project中的字段也需要合并,因为开始,结束日期和分配对于员工和项目都是唯一的。

然后映射合并数据中的组数组:

const data = [{"employeeProjectId":1,"project_role":"a"},{"employeeProjectId":1,"project_role":"b"},{"employeeProjectId":3,"project_role":"a"},{"employeeProjectId":3,"project_role":"c"}];
const groupBy = (getGroup,array) => {
  //use getGroup function to get the group identifyer of the items (is employeeProjectId)
  const groups = array.map(getGroup);
  return array.reduce(//reduce array into array of arrays that are grouped
    (result,item,index)=>{
      //get the group of current item
      const itemGroup = getGroup(item);
      //find the index of this item in groups
      const indexOfGroup = groups.indexOf(itemGroup);
      //index of this item in groups is current index so it's first item of the group
      if(indexOfGroup===index){
        return result.concat([[item]])
      }
      //it is not first item of the group, add this item to it's correct group
      //add current item to the right group
      result.find(([item])=>getGroup(item)===itemGroup).push(item);
      return result;
    },
    []//initial result
  )
}

const merge = (item1,item2) => {
  //if project_role is not an array yet, make it an array
  if(!Array.isArray(item1.project_role)){
    item1 = {...item1,project_role:[item1.project_role]};
  }
  //add project_role of item2 to item1
  item1.project_role.push(item2.project_role);
  return item1;
};

const grouped = groupBy(x=>x.employeeProjectId,data);
console.log("grouped",grouped);

console.log(
  "grouped and merged",
  groupBy(x=>x.employeeProjectId,data).map(
    group=>
      group.reduce(merge)
  )
)

您可能不熟悉某些语法,例如spread in object literals来复制对象(不是深层副本),arrow functionsdestructuring assignment试图在对象中查找元素时使用的语法数组。

map和reduce方法已记录在here

答案 1 :(得分:0)

我想您正在寻找类似这样的东西:D

否则,您也可以使用lodash模块。

    const obj = {
    "code": 200,
    "success": true,
    "message": "Successfully completed",
    "data": [
        {
            "employeeProjectId": 1,
            "projectRoleId": 1,
            "employee_project": {
                "id": 1,
                "status": true,
                "type": "backup",
                "startDate": "2018-06-28T07:20:36.000Z",
                "endDate": "2018-06-29T07:20:39.000Z",
                "allocation": 100,
                "employeeId": 326,
                "projectId": 35,
                "employee": {
                    "firstName": "Asad",
                    "lastName": "Marfani"
                },
                "project": {
                    "name": "RTA"
                }
            },
            "project_role": {
                "id": 1,
                "role": "front_end_ios"
            }
        },
        {
            "employeeProjectId": 1,
            "projectRoleId": 2,
            "employee_project": {
                "id": 1,
                "status": true,
                "type": "backup",
                "startDate": "2018-06-28T07:20:36.000Z",
                "endDate": "2018-06-29T07:20:39.000Z",
                "allocation": 100,
                "employeeId": 326,
                "projectId": 35,
                "employee": {
                    "firstName": "Asad",
                    "lastName": "Marfani"
                },
                "project": {
                    "name": "RTA"
                }
            },
            "project_role": {
                "id": 2,
                "role": "Front End - Android"
            }
        },
        {
            "employeeProjectId": 3,
            "projectRoleId": 1,
            "employee_project": {
                "id": 3,
                "status": true,
                "type": "backup",
                "startDate": "2018-06-28T07:47:19.000Z",
                "endDate": "2018-06-29T07:47:22.000Z",
                "allocation": 50,
                "employeeId": 16,
                "projectId": 35,
                "employee": {
                    "firstName": "Nosheen",
                    "lastName": "Sikandar"
                },
                "project": {
                    "name": "RTA"
                }
            },
            "project_role": {
                "id": 1,
                "role": "front_end_ios"
            }
        },
        {
            "employeeProjectId": 3,
            "projectRoleId": 3,
            "employee_project": {
                "id": 3,
                "status": true,
                "type": "backup",
                "startDate": "2018-06-28T07:47:19.000Z",
                "endDate": "2018-06-29T07:47:22.000Z",
                "allocation": 50,
                "employeeId": 16,
                "projectId": 35,
                "employee": {
                    "firstName": "Nosheen",
                    "lastName": "Sikandar"
                },
                "project": {
                    "name": "RTA"
                }
            },
            "project_role": {
                "id": 3,
                "role": "Front End - Web"
            }
        }
    ]}
    
    const data = obj.data
    const res = data.filter(emp => {
    	return emp.employee_project.id == "1"
    }).reduce(item => {
    	return item
    })
    
    console.log(res)