0

Well, I need to make an array concatenation, placed inside the Object.

So the worked solution I've made is:

      const usersList = {
        tester: [{ id: 1, //... }, //...],
        custumers: [{ id: 1, //... }, //...],
        admin: [{ id: 1, //... }, //...]
      }

      let allUsers = []

      Object.keys(usersList).forEach(listKey => {
        allUsers = [
          ...allUsers,
          ...usersList[listKey]
        ]
      })

      return allUsers

Besides, I wonder perhaps there is present a much fashionable way to deal with such a case? I tried this one, but it doesn't work:

[...Object.keys(usersList).map(listKey => usersList[listKey])]

2 Answers 2

3

Take the object's values, which will give you an array of arrays, then flatten:

const allUsers = Object.values(usersList).flat();

If you can't use .flat, then:

const allUsers = [].concat.apply(...Object.values(usersList));
Sign up to request clarification or add additional context in comments.

Comments

0

Another way is to use flatMap

const usersList = {
  tester: [{ id: 1 }],
  custumers: [{ id: 1 }],
  admin: [{ id: 1 }]
};

const res = Object.values(usersList).flatMap(x => x);

console.log(res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.