{"componentChunkName":"component---src-templates-post-js","path":"/blog/2020/10/05/array-findindex/","result":{"data":{"post":{"html":"<p>Searching for items in an array has been the point of discussion for many years and debate on what is the best and optimum way for searching for objects has had many solutions, some effective and some not.</p>\n<p>However, with the all new <code class=\"language-text\">findIndex</code> method on <code class=\"language-text\">Array.prototype</code> you have the flexibility to search for objects using your own comparison callback method.</p>\n<!--more-->\n<h2 id=\"the-what\" style=\"position:relative;\"><a href=\"#the-what\" aria-label=\"the what permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The what</h2>\n<p>The <code class=\"language-text\">findIndex</code> method returns the index of the first element in an array if the callback method passed to it returns <code class=\"language-text\">true</code>, otherwise it returns <code class=\"language-text\">-1</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"69284524062846080000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const isPerfectSquare = (num) => {\r\n  return num > 0 && Math.sqrt(num) % 1 === 0;\r\n}\r\n\r\nconsole.log([1, 3, 8, 9, 12].findIndex(isPerfectSquare)); // 1\r\nconsole.log([1, 6, 7, 10, 14].findIndex(isPerfectSquare)); // -1`, `69284524062846080000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">isPerfectSquare</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">num</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">return</span> num <span class=\"token operator\">></span> <span class=\"token number\">0</span> <span class=\"token operator\">&amp;&amp;</span> Math<span class=\"token punctuation\">.</span><span class=\"token function\">sqrt</span><span class=\"token punctuation\">(</span>num<span class=\"token punctuation\">)</span> <span class=\"token operator\">%</span> <span class=\"token number\">1</span> <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">8</span><span class=\"token punctuation\">,</span> <span class=\"token number\">9</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">findIndex</span><span class=\"token punctuation\">(</span>isPerfectSquare<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 1</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">6</span><span class=\"token punctuation\">,</span> <span class=\"token number\">7</span><span class=\"token punctuation\">,</span> <span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token number\">14</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">findIndex</span><span class=\"token punctuation\">(</span>isPerfectSquare<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// -1</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>There are two facts you should know:</p>\n<ul>\n<li>This method does not run once it found the first matching element.</li>\n<li>It does not change the original array.</li>\n</ul>\n<h2 id=\"syntax\" style=\"position:relative;\"><a href=\"#syntax\" aria-label=\"syntax permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Syntax</h2>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"18333125617480817000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`array.findIndex(function(currentValue, index, arr), thisValue);`, `18333125617480817000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\">array<span class=\"token punctuation\">.</span><span class=\"token function\">findIndex</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">currentValue<span class=\"token punctuation\">,</span> index<span class=\"token punctuation\">,</span> arr</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> thisValue<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"parameters\" style=\"position:relative;\"><a href=\"#parameters\" aria-label=\"parameters permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Parameters</h3>\n<ul>\n<li><strong>function:</strong> This is your callback function which checks for a condition to match the element you’re after.</li>\n<li><strong>currentValue:</strong> This holds the current element’s value.</li>\n<li><strong>index:</strong> This is an optional parameter which holds the current index.</li>\n<li><strong>arr:</strong> This is also an optional parameter which holds the array that the current element belongs to.</li>\n<li><strong>thisValue:</strong> Yet another optional parameter, if a value is passed, it will be used as <code class=\"language-text\">this</code> value inside the function, otherwise <code class=\"language-text\">undefined</code> will be passed.</li>\n</ul>\n<h3 id=\"return-value\" style=\"position:relative;\"><a href=\"#return-value\" aria-label=\"return value permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Return value</h3>\n<p>It will return the index of the found item or <code class=\"language-text\">-1</code> if the callback function is not satisfied.</p>\n<h2 id=\"how-it-works-under-the-hood\" style=\"position:relative;\"><a href=\"#how-it-works-under-the-hood\" aria-label=\"how it works under the hood permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How it works under the hood?</h2>\n<p>When the <code class=\"language-text\">findIndex</code> is called with one or two arguments, the following steps are executed:</p>\n<ol>\n<li>Let <code class=\"language-text\">O</code> be ? <code class=\"language-text\">ToObject(this value)</code></li>\n<li>Let <code class=\"language-text\">len</code> be ? <code class=\"language-text\">LengthOfArrayLike(O)</code></li>\n<li>If <code class=\"language-text\">IsCallback(predicate)</code> is <code class=\"language-text\">false</code>, throw a <strong>TypeError</strong> exception</li>\n<li>Let <code class=\"language-text\">k</code> be 0</li>\n<li>Repeat, while <code class=\"language-text\">k &lt; len</code></li>\n</ol>\n<ul>\n<li>Let <code class=\"language-text\">Pk</code> be ! <code class=\"language-text\">ToString(k)</code></li>\n<li>Let <code class=\"language-text\">kValue</code> be ? <code class=\"language-text\">Get(O, Pk)</code></li>\n<li>Let <code class=\"language-text\">testResult</code> be ! <code class=\"language-text\">ToBoolean(? Call(predicate, thisArg, « kValue, k, O »))</code></li>\n<li>If <code class=\"language-text\">testResult</code> is <code class=\"language-text\">true</code>, return <code class=\"language-text\">k</code></li>\n<li>Set <code class=\"language-text\">k</code> to <code class=\"language-text\">k + 1</code></li>\n</ul>\n<ol start=\"6\">\n<li>Return <code class=\"language-text\">-1</code></li>\n</ol>","timeToRead":1,"frontmatter":{"unformattedDate":"2020-10-05T00:00:00.000Z","date":"October 05, 2020","title":"Find an item in an array with the new 'findIndex' method 🔎","subtitle":null,"id":null,"author":"Yaser Adel Mehraban","gravatar":null,"canonical_url":null,"tags":["showdev","webdev","javascript","findindex"],"thumbnail":null,"cover":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB90lEQVR42i2OS2vbQBRG9au76H/ortDu2k1pN4VCY0gTnMQ08cjWjO04jiRbsh6WZEmjeehtPRLRcSgcDt/l8l2utHaZFpVaVOxw7dCTx/7jsvPo0iZImzBvj1kbpG10duOQKmAnB2cSCjKD9TvaLpzk8cCMuLBwIdY2qS1SG7i0aWMlpRtnbpJ7tDLj/Ji/kHrwWS1N/XSVNCo+Xc7WV/PnsaL9vLy7uJVHE2U8e/pzj36LfDv9cYMuZ5u5at3BzUG8UA4uqSTgpxCfUHza8t7JXve0mSz1yUK7ltcL4wierbGiPqx3U7fQWO+VQ1ANdtrtWK9FohykKK6hm0KLL91cDSsnewmq17AeonoQ9qtBdA5Zb/HWpK2wwTqddpuwkMA+QRZXDAbtFLo5coqFmz96+TY+7UlrknaXNDppdN5t3zo663a8FfkpLCWgx0ubr/YU2tnMpNMtftCxYueKW8AzJTyUMKggbhDpFqSbHVpgNSoV5UKS93z+AFZXvxYWv1naI6BeAFW2OfJKKPDffKaAx3pJmi/f+nfv+2nQojCXoJONxuDTx8+ymQADzx0+sxmwGPIq5JdnvBIFFTpW0K9WuP5+3X342ilhKztcut/4020y0bBsEiDYU+Bw2cCyHsrbSNaEY2BRIC4KTDJ3CQyobLG/WvQPPnkMF8vEHrMAAAAASUVORK5CYII="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/0a7f38c1d4bc3ece7787ff19bd4d0a70/a2195/socialpreview.png","srcSet":"/static/0a7f38c1d4bc3ece7787ff19bd4d0a70/cadb4/socialpreview.png 750w,\n/static/0a7f38c1d4bc3ece7787ff19bd4d0a70/a2195/socialpreview.png 800w","sizes":"(min-width: 800px) 800px, 100vw"},"sources":[{"srcSet":"/static/0a7f38c1d4bc3ece7787ff19bd4d0a70/11ba1/socialpreview.webp 750w,\n/static/0a7f38c1d4bc3ece7787ff19bd4d0a70/5f042/socialpreview.webp 800w","type":"image/webp","sizes":"(min-width: 800px) 800px, 100vw"}]},"width":800,"height":400}}},"img":null,"socialPreview":{"publicURL":"/static/0a7f38c1d4bc3ece7787ff19bd4d0a70/socialpreview.png"}}}},"pageContext":{"title":"Find an item in an array with the new 'findIndex' method 🔎","postPath":"/blog/2020/10/05/array-findindex","nextPost":"/blog/2020/10/09/powertoys","previousPost":"/blog/2020/09/06/image-capture-api"}},"staticQueryHashes":[],"slicesMap":{}}