Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Skip over element in .map without adding .filter etc

I'm looking for beautiful solution I have array items. Item consists of user_id and some other information. I need map all items and for each item find user with appropriate id. Then add to item some information from user

items = items.map((item) => {
                let user = users.find(u => item.user_id === u.id);

                item.email = user.email;
                item.user_name = user.name;
                return item;
            });

But if user with item.user_id doesn't exist I must do nothing. I already have iterations of two arrays and don't want add more

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

It depends what you mean by "skip over element."

If you mean "don't include it in the output array," then you'll have to put map aside, because map will always produce an output element for an input element. Instead, you can just loop through pushing to a new array:

const originalItems = items;
items = [];
for (const item of originalItems) {
    let user = users.find(u => item.user_id === u.id);
    if (user) {
        item.email = user.email;
        item.user_name = user.name;
        items.push(item);
    }
}

If you just mean "don't add the user information" (but do keep items you don't add information to), then you can just branch:

items = items.map((item) => {
    let user = users.find(u => item.user_id === u.id);
    if (user) {
        item.email = user.email;
        item.user_name = user.name;
    }
    return item;
});

That raises the question of why you're using map at all, though, since there doesn't immediately seem to be much reason to create a new array with the same objects in it (even if we're adding to those objects). If you're using something like React where state updates must not modify existing objects, you'd want to create a new item for any item you changed instead:

items = items.map((item) => {
    let user = users.find(u => item.user_id === u.id);
    if (user) {
        item = {
            ...item,
            email: user.email,
            user_name = user.name,
        };
    }
    return item;
});

But you may not be doing that, you haven't said.

about 4 years ago · Santiago Trujillo Report

0

Maybe try:

array.map(x => condition ? parse(x) : x);

smth. like that

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!