简体   繁体   中英

How to concatenate indefinite number of arrays of Javascript objects

I have an array of Groups st each Group has many Users

I want to return all (unique) Users for a given array of Groups.

So far, I have

  let actor = await User.query().findById(req.user.id).eager('groups') // find the actor requesting


let actor_groups = actor.groups // find all groups of actor


  if (actor_groups.length > 1)
    var actor_groups_users = actor_groups[0].user
    for (let i = 0; i < actor_groups.length; i++) {
      const actor_groups_users = actor_groups_users.concat(actor_groups[i]);
    }
    console.log('actor groups users is', actor_groups_users);
  else 
    // return users from the first (only) group

which returns the error: actor_groups_users is not defined

Feels like a roundabout way to do this. Is there a way to just combine actor_groups into a single combined group?

You can simply do this:

const allGroupsArrs = actor_groups.map(({ user }) => user);
const actor_groups_users = [].concat(...allGroupArrs);

Or, you could simply use the .flat() method, which is not yet officially part of the ES standard, but is on its way there and has browser support outside of IE:

const allGroupsArrs = actor_groups.map(({ user }) => user);
const actor_groups_users = allGroupArrs.flat();

Also, the above would result in duplicate values in actor_groups_users if there are people who are in multiple groups. You can remedy this (assuming the array elements are primitive values) using a Set :

const unique_users = [...new Set(actor_groups_users)];

Here we can cycle through, adding users if not already in the array, using .forEach() and .includes() .

This is assuming that group.user is an Array of users.

let users = [];

// check that actor_groups has items in it
if (actor_groups && actor_groups.length > 1) {

   // cycle through each actor_group
   actor_groups.forEach( group => {

      // check if we have a 'user' array with items in it
      if (group.user && group.user.length > 1) {

         // cycle through each user in the group
         group.user.forEach( user => {

            // check if we already have this user
            // if not, add it to users
            if (!users.includes(user)) {
               users.push(user);
            }
         }
      }
   }
}

The most efficient way I can think of is

const users = [...new Set([...actor_groups].flatMap(el => el.user))]

I used this example:

const actor_groups = [{user: ['ok','boom']}, {user: 'er'}]
console.log([...new Set([...actor_groups].flatMap(el => el.user))])
//output: ["ok", "boom", "er"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM