Home > Enterprise >  JS - Apply limit on array of object
JS - Apply limit on array of object

Time:02-24

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related