Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

46
Views
Javascript arrays better performance

I have an array of objects (myObjsArr). The object contains 2 methods:

obj: {
   getUserId(), // returns a string. For instance, "123"
   getSubUserIds(), // returns an array of strings. For instance, ["abc", "abc2", ...]
}

I'm looping this array of objects to create 2 arrays.

  1. Array of user ids
  2. Array of sub user ids.
const userIds = [];
const subUserIds = [];

myObjsArr.forEach((obj) => {
   userIds.push(obj.getUserId());
   obj.getSubUserIds(element => {
      subUserIds.push(element);
   })
});

This works, but seems not efficient. How could I improve my code?

Thanks

7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs