Find First N Matches

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the findFirstN() function in JavaScript. This function is used to find the first n elements in an array for which a given function returns a truthy value. We will learn how to use findFirstN() in combination with other array methods to manipulate and extract specific data from arrays.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/loops("`Loops`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/BasicConceptsGroup -.-> javascript/obj_manip("`Object Manipulation`") subgraph Lab Skills javascript/variables -.-> lab-28303{{"`Find First N Matches`"}} javascript/data_types -.-> lab-28303{{"`Find First N Matches`"}} javascript/arith_ops -.-> lab-28303{{"`Find First N Matches`"}} javascript/comp_ops -.-> lab-28303{{"`Find First N Matches`"}} javascript/cond_stmts -.-> lab-28303{{"`Find First N Matches`"}} javascript/loops -.-> lab-28303{{"`Find First N Matches`"}} javascript/array_methods -.-> lab-28303{{"`Find First N Matches`"}} javascript/obj_manip -.-> lab-28303{{"`Find First N Matches`"}} end

How to Find the First N Matches

To find the first n elements that meet a certain criteria, use the findFirstN function. Here's how:

  1. Open the Terminal/SSH.
  2. Type node to start practicing coding.
  3. Use the findFirstN function, passing in the array to search through, a matching function, and the number of matches to find (if not specified, the default is 1).
  4. The matcher function will be executed for each element of the arr, and if it returns a truthy value, that element will be added to the results array.
  5. If the res array reaches a length of n, the function will return the results array.
  6. If no matches are found, an empty array will be returned.

Here's the code for the findFirstN function:

const findFirstN = (arr, matcher, n = 1) => {
  let res = [];
  for (let i in arr) {
    const el = arr[i];
    const match = matcher(el, i, arr);
    if (match) res.push(el);
    if (res.length === n) return res;
  }
  return res;
};

And here are some examples of how to use it:

findFirstN([1, 2, 4, 6], (n) => n % 2 === 0, 2); // [2, 4]
findFirstN([1, 2, 4, 6], (n) => n % 2 === 0, 5); // [2, 4, 6]

Summary

Congratulations! You have completed the Find First N Matches lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like