Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda