Jump to content

JS Goupby-like algorithm for Object arrays

Go to solution Solved by keskparane,

iterate through the first array and save the elapsed time sums to a new object with their id

iterate through the second array and save the project ids to a new object with the corresponding elapsed time

you now have an object that can access elapsed times by project id

Hey guys!

 

I have run into at problem where I have two different arrays:

 


// Sample data from function getTimeIntervals()

[

  RowDataPacket { issue_id: 117648, seconds_elapsed_issue: '318' },
  RowDataPacket { issue_id: 118090, seconds_elapsed_issue: '8940' },
  RowDataPacket { issue_id: 118092, seconds_elapsed_issue: '5357' },
  RowDataPacket { issue_id: 118199, seconds_elapsed_issue: '41604' },
  RowDataPacket { issue_id: 118351, seconds_elapsed_issue: '3778' },
  RowDataPacket { issue_id: 118395, seconds_elapsed_issue: '2974' },
  RowDataPacket { issue_id: 119014, seconds_elapsed_issue: '37680' },
  RowDataPacket { issue_id: 119729, seconds_elapsed_issue: '15006' },
  RowDataPacket { issue_id: 119735, seconds_elapsed_issue: '26389' },
  RowDataPacket { issue_id: 119742, seconds_elapsed_issue: '67' },

  ...
 ]

 

// Sample data from function getIssueData()

[

  RowDataPacket { project_id: 1445, issue_id: 121007 },
  RowDataPacket { project_id: 1445, issue_id: 120921 },
  RowDataPacket { project_id: 1445, issue_id: 121011 },
  RowDataPacket { project_id: 1420, issue_id: 120454 },
  RowDataPacket { project_id: 1420, issue_id: 121004 },
  RowDataPacket { project_id: 1445, issue_id: 120868 },
  RowDataPacket { project_id: 1467, issue_id: 121010 },
  RowDataPacket { project_id: 1445, issue_id: 120870 },
  RowDataPacket { project_id: 1420, issue_id: 121009 },
  RowDataPacket { project_id: 1445, issue_id: 120018 },

  ...

]

 

Now... What I want to do is avoid using a nested for loop for this because using nested for loops is in my opinion the slowest possible way to solve this task (O(n^2) time if I'm not mistaken): I want to add up all elapsed seconds from an issue under the 'project_id' value from the second array.

 

This means producing a result that looks something like this:

 


[

  {project_id: 1445, seconds_elapsed: '270129'},

  {project_id: 1467, seconds_elapsed: '16012'},

  ...

]

 

Does anyone have a solution to this particular problem? Because those I have found out there on the internet, I either do not understand or does not apply to my use case.

-- Thanks!

A simple software developer from the far away land of Denmark

Link to comment
Share on other sites

Link to post
Share on other sites

Use a foreach statement

Ryzen 5 1600 @ 3.9 Ghz  | Gigabyte AB350M Gaming 3 |  PaliT GTX 1050Ti  |  8gb Kingston HyperX Fury @ 2933 Mhz  |  Corsair CX550m  |  1 TB WD Blue HDD


Inside some old case I found lying around.

 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, bleedblue said:

Use a foreach statement

If I use a foreach statement I still have to nest the statement right? Or am I wrong there? Please specify

A simple software developer from the far away land of Denmark

Link to comment
Share on other sites

Link to post
Share on other sites

I would have thought there would be 2 options.

  1. For elapsed seconds of a specific project match items with the issue id of first array.
  2. For elapsed seconds of each project add first array items then apply to second array items.

Either way only requires a single pass on each array.

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, keskparane said:

I would have thought there would be 2 options.

  1. For elapsed seconds of a specific project match items with the issue id of first array.
  2. For elapsed seconds of each project add first array items then apply to second array items.

Either way only requires a single pass on each array.

So would you just have me do a pass of each array gather up data and then loop again? I'm not sure what you mean here..

A simple software developer from the far away land of Denmark

Link to comment
Share on other sites

Link to post
Share on other sites

Well do you want elapsed time of each project or just elapsed time for a specific project?

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, keskparane said:

Well do you want elapsed time of each project or just elapsed time for a specific project?

I want elapsed time for each project calculated by the issues's seconds elapsed

A simple software developer from the far away land of Denmark

Link to comment
Share on other sites

Link to post
Share on other sites

iterate through the first array and save the elapsed time sums to a new object with their id

iterate through the second array and save the project ids to a new object with the corresponding elapsed time

you now have an object that can access elapsed times by project id

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, keskparane said:

iterate through the first array and save the elapsed time sums to a new object with their id

iterate through the second array and save the project ids to a new object with the corresponding elapsed time

you now have an object that can access elapsed times by project id

Ah - that's what you meant :D Thanks - will implement ?

A simple software developer from the far away land of Denmark

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×