Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
JS - Apply limit on array of object

i have a simple array of object in js

const users = [
   {categories: 'FRENCH', user: [{id: 1}, {id: 2}]}, 
   {categories: 'BELGIUM', user: [{id: 5}, {id: 3}, {id:4}]},
   {categories: 'USA', user: [{id: 12}, {id: 13}, {id:15}]}  
]

I want to limit this array depending on user size So for example if i want only 3 user a function name limitTo(users, 3) should return :

const only3users = [
   {categories: 'FRENCH', user: [{id: 1}, {id: 2}]}, 
   {categories: 'BELGIUM', user: [{id: 5}]}, 
]

What's the best algorithm to achieve this ?

Thanks

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Probably not the best way but working :

const users = [
  {categories: 'FRENCH', user: [{id: 1}, {id: 2}]}, 
  {categories: 'BELGIUM', user: [{id: 5}, {id: 3}, {id:4}]},
  {categories: 'USA', user: [{id: 12}, {id: 13}, {id:15}]}  
]

function limitTo(arr, n) {
  let stack = []
  const res = []
  for (let i = 0; i < arr.length; i++) {
    let item = arr[i]
    if (stack.length === n) return res
    if (item.user.length < n - stack.length) {
      res.push(item)
      stack = [...stack, ...item.user]
    } else {
      const rest = n - stack.length
      const users = item.user.splice(0, rest)
      item = { ...item, user: users }
      res.push(item)
      stack = [...stack, ...users]
    }
  }

  return res
}

limitTo(users, 3)

We use a stack array that stores the users when that we decide to add to our response. We compare the users length for each object of the array with the N number of users you want minus the stack length (already added users). If our response can deal with all the users, we add them all. Else, we splice the user array by the proper number.

about 4 years ago · Juan Pablo Isaza Denunciar

0

One fairly simple recursive version would look like this:

const limitTo = ([{user, ...rest} = {}, ...xs], n) =>
  user 
    ? user .length >= n 
      ? [{... rest, user: user .slice (0, n)}]
      : [{... rest, user}, ... limitTo (xs, n - user .length)]
    : []

const users = [{categories: 'FRENCH', user: [{id: 1}, {id: 2}]}, {categories: 'BELGIUM', user: [{id: 5}, {id: 3}, {id:4}]}, {categories: 'USA', user: [{id: 12}, {id: 13}, {id:15}]}];

[3, 5, 7, 20] .forEach (n => console .log (`Count ${n}:`, limitTo (users, n)))
.as-console-wrapper {max-height: 100% !important; top: 0}

If we have no more root objects, then user will be undefined and so we return an empty array. Otherwise, we compare the number of users in the object. If it's greater than or equal to the target, n, we return an object with a user array containing the first n pieces. Otherwise we include the current object as well as the recursive results for the remaining objects and a target reduced by the number of users in the current object.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda