简体   繁体   English

如何在 Javascript 中进行 map 阵列一对多关系

[英]How to map array one to many relationship in Javascript

I am trying to convert following JSON data using map function into appropriate output我正在尝试使用 map function 将以下 JSON 数据转换为适当的 Z78E6221F6393D1356681CEDB481CE

[{"jobId":100049,"name":"Drilling","jobNumber":"1222455","address":"Ahmedabad","city":"Ahmedabad","state":"1","zip":"38003","active":true,"jobOwnerId":100002,"jobOwnerName":"Troy Thomson","createdBy":"","createdOn":"2020-03-15T18:42:25.6533333","modifiedBy":"","modifiedOn":"2020-03-16T13:21:53.0333333","members":[{"memberId":100001,"memberName":"Hardik Gondalia"},{"memberId":100004,"memberName":"Micheal T. Angelo"}],"assets":[{"assetId":100004,"assetName":"Minima ullam non mol"}]},{"jobId":100051,"name":"Drilling The Hole","jobNumber":"11111","address":"201, AA 333 Steet, Time Square","city":"Boston","state":"1","zip":"11111","active":true,"jobOwnerId":100001,"jobOwnerName":"Hardik Gondalia","createdBy":"","createdOn":"2020-03-25T11:12:13.89","modifiedBy":"","modifiedOn":"2020-03-25T11:12:32.1266667","members":[{"memberId":100002,"memberName":"Troy Thomson"}],"assets":[{"assetId":100005,"assetName":"Drill Machine P2222"}]}]

Desired Output:所需的 Output:

[{"jobid":100049,"memberid":100001],{"jobid":100049,"memberid":100004],{"jobid":100051,"memberid":100002]}

As you can see job is json object and have array of members and assets inside it.如您所见,工作是 json object,其中包含一系列成员和资产。 I want to repeat jobid if it has multiple member or assets.如果它有多个成员或资产,我想重复 jobid。

What I've tried is:我试过的是:

const assignmentAssetModel = this.jobList.map(i => ({ jobid: i.jobId, assetid: i.assets.map(j => j.assetId) }));

But it gives me following output:但它让我关注 output:

[{"jobid":100049,"memberid":[100001,100004]},{"jobid":100051,"memberid":[100002]}]

As you can I want to repeat jobid if it has more then one memberid你可以我想重复 jobid 如果它有超过一个 memberid

You can use Array.reduce() instead of Array.map() since the length of the result array is different then the initial array.您可以使用Array.reduce()而不是Array.map()因为结果数组的长度与初始数组不同。

 const jobList = [{"jobId":100049,"name":"Drilling","jobNumber":"1222455","address":"Ahmedabad","city":"Ahmedabad","state":"1","zip":"38003","active":true,"jobOwnerId":100002,"jobOwnerName":"Troy Thomson","createdBy":"","createdOn":"2020-03-15T18:42:25.6533333","modifiedBy":"","modifiedOn":"2020-03-16T13:21:53.0333333","members":[{"memberId":100001,"memberName":"Hardik Gondalia"},{"memberId":100004,"memberName":"Micheal T. Angelo"}],"assets":[{"assetId":100004,"assetName":"Minima ullam non mol"}]},{"jobId":100051,"name":"Drilling The Hole","jobNumber":"11111","address":"201, AA 333 Steet, Time Square","city":"Boston","state":"1","zip":"11111","active":true,"jobOwnerId":100001,"jobOwnerName":"Hardik Gondalia","createdBy":"","createdOn":"2020-03-25T11:12:13.89","modifiedBy":"","modifiedOn":"2020-03-25T11:12:32.1266667","members":[{"memberId":100002,"memberName":"Troy Thomson"}],"assets":[{"assetId":100005,"assetName":"Drill Machine P2222"}]}] const jobsPerMember = jobList.reduce((acc, cur) => { cur.members.forEach((member) => acc.push({ jobid: cur.jobId, memberid: member.memberId })) return acc },[]) console.log(jobsPerMember)

You could take Array#flatMap and map the outer and inner properties.您可以将Array#flatMap和 map 用作外部和内部属性。

 var data = [{ jobId: 100049, name: "Drilling", jobNumber: "1222455", address: "Ahmedabad", city: "Ahmedabad", state: "1", zip: "38003", active: true, jobOwnerId: 100002, jobOwnerName: "Troy Thomson", createdBy: "", createdOn: "2020-03-15T18:42:25.6533333", modifiedBy: "", modifiedOn: "2020-03-16T13:21:53.0333333", members: [{ memberId: 100001, memberName: "Hardik Gondalia" }, { memberId: 100004, memberName: "Micheal T. Angelo" }], assets: [{ assetId: 100004, assetName: "Minima ullam non mol" }] }, { jobId: 100051, name: "Drilling The Hole", jobNumber: "11111", address: "201, AA 333 Steet, Time Square", city: "Boston", state: "1", zip: "11111", active: true, jobOwnerId: 100001, jobOwnerName: "Hardik Gondalia", createdBy: "", createdOn: "2020-03-25T11:12:13.89", modifiedBy: "", modifiedOn: "2020-03-25T11:12:32.1266667", members: [{ memberId: 100002, memberName: "Troy Thomson" }], assets: [{ assetId: 100005, assetName: "Drill Machine P2222" }] }], result = data.flatMap(({ jobId, members }) => members.map(({ memberId }) => ({ jobId, memberId }))); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

You should use the .reduce method instead:您应该改用.reduce方法:

this.jobList.reduce((acc, { members, jobId }) => {
   return [...acc, ...members.map(({ memberId }) => ({ jobId, memberId }))]; 
}, []);

If you're targeting more recent version of javascript (ES2019), you can also use flatMap:如果您的目标是更新版本的 javascript (ES2019),您还可以使用 flatMap:

this.jobList.flatMap(({ members, jobId }) => members.map(({ memberId }) => ({ jobId, memberId })));

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

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