简体   繁体   中英

How to map array one to many relationship in Javascript

I am trying to convert following JSON data using map function into appropriate output

[{"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:

[{"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. I want to repeat jobid if it has multiple member or assets.

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:

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

As you can I want to repeat jobid if it has more then one memberid

You can use Array.reduce() instead of Array.map() since the length of the result array is different then the initial array.

 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.

 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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