Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Iterate array of objects by dimension, based on given limit

I have a multi-dimensional array of objects which contain suggested users. Again each suggested user has some suggested users and so on.

In the application I'm building I select a user (let's call him 'Steve') and would like to receive a certain amount of users connected to that user (by fetching the suggestions). The data is all there, I just need to find a meaningful way to iterate this.

To simplify I have built an array with some sample data where each user has two suggested users:

// Suggested users of my initially selected user (Steve)
let suggested_users_based_on_steve = [
    {
        name: 'A',
        suggested: [
            {
                name: 'A1',
                suggested: [
                    { name: 'A1A1' },
                    { name: 'A1A2' }
                ]
            },
            {
                name: 'A2',
                suggested: [
                    { name: 'A2A1' },
                    { name: 'A2A2' }
                ]
            }
        ]
    },
    {
        name: 'B',
        suggested: [
            {
                name: 'B1',
                suggested: [
                    { name: 'B1B1' },
                    { name: 'B1B2' }
                ]
            },
            {
                name: 'B2',
                suggested: [
                    { name: 'B2B1' },
                    { name: 'B2B2' }
                ]
            }
        ]
    }
];
  • Let's say for example I need to get 7 suggested users based on my user Steve. That means I would iterate the first dimension and get the first two users (A and B).
  • Since now I have fetched only 2 of 7 users, I need to go one dimension deeper and get the suggested users of A and B which are A1, A2, B1 and B2. This would add up to 6 out of 7 required users, so I need one more.
  • Therefore I also fetch one suggested user of A1, which is A1A1. Now I need to break / stop the iteration because I'm done.

Please note that the amount of suggested users is not fixed. Every user can have an unlimited amount of suggested users. Also, it's important iterate from layer to layer to get the 'best' and most connected results / users. The initial user is more connected to A and B than to A1A1 and A1A2.

I'm struggling with finding the right combination of nested for...of loops. Is there any recommended way on doing this?

Thank you in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

let suggested_users_based_on_steve = [
  {
    name: 'A',
    suggested: [
      {
        name: 'A1',
        suggested: [
          { name: 'A1A1' },
          { name: 'A1A2' }
        ]
      },
      {
        name: 'A2',
        suggested: [
          { name: 'A2A1' },
          { name: 'A2A2' }
        ]
      }
    ]
  },
  {
    name: 'B',
    suggested: [
      {
        name: 'B1',
        suggested: [
          { name: 'B1B1' },
          { name: 'B1B2' }
        ]
      },
      {
        name: 'B2',
        suggested: [
          { name: 'B2B1' },
          { name: 'B2B2' }
        ]
      }
    ]
  }
];

function test(arr, result = []) {
  for (let user of arr.values()) {
    result.push(user);
    if (result.length === 7) return result;
  }
  var next = arr.reduce(function (all, user) {
    return all.concat(user.suggested || []);
  }, []);
  if (next.length === 0) return result;
  return test(next, result);
}

test(suggested_users_based_on_steve);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!