edit-article
Home
Up
Delete
Article Name:
Article Description:
by Dhananjay Kumar - How to find index of an item in JavaScript Object Array - a simple function that will return the index of any object in an any given array of object, given the "key" and the "value" to match
Chapter ID/Name:
Status:
Write
Writing
Written
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">lookup the index of an object</h1> <h2>[WHAT]</h2> <ol> <li>] a javascript function to find any object in an array of objects, using any element in the</li> </ol> <h2>[WHY]</h2> <ol> <li>] lookup the index of a particular element in an array of javascript objects</li> </ol> <h2>[WHERE]</h2> <ol> <li><strong>] READ THE FULL ARTICLE</strong></li> <ol> <li>] <a href="http://debugmode.net/2013/02/19/how-to-find-index-of-an-item-in-javascript-object-array/" target="_blank">http://debugmode.net/2013/02/19/how-to-find-index-of-an-item-in-javascript-object-array/</a> </li> </ol> <li>]</li> </ol> <h2>[WHEN]</h2> <ol> <li>] project-4800-pass - IN routes/index - selections</li> </ol> <h2>[EXAMPLE]</h2> <ol> <li>]</li> </ol> <h2>[HOW-TO]</h2> <ol> <li><strong>] define function</strong></li> <li> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"> <span style="color: #4f76ac;">function</span> findIndexByKeyValue<span style="color: #1e7c70;">(</span>searchDrivers<span style="color: #1e7c70;">,</span> key<span style="color: #1e7c70;">,</span> valuetosearch<span style="color: #1e7c70;">)</span> <span style="color: #1e7c70;">{</span> <span style="color: #4f76ac;">for</span> <span style="color: #1e7c70;">(</span><span style="color: #4f76ac;">var</span> i <span style="color: #1e7c70;">=</span> <span style="color: #9b5f9b;">0</span><span style="color: #1e7c70;">;</span> i <span style="color: #1e7c70;"><</span> searchDrivers.length<span style="color: #1e7c70;">;</span> i<span style="color: #1e7c70;">++)</span> <span style="color: #1e7c70;">{</span> <span style="color: #4f76ac;">if</span> <span style="color: #1e7c70;">(</span>searchDrivers<span style="color: #1e7c70;">[</span>i<span style="color: #1e7c70;">][</span>key<span style="color: #1e7c70;">]</span> <span style="color: #1e7c70;">==</span> valuetosearch<span style="color: #1e7c70;">)</span> <span style="color: #1e7c70;">{</span> <span style="color: #4f76ac;">return</span> i<span style="color: #1e7c70;">;</span> <span style="color: #1e7c70;">}</span> <span style="color: #1e7c70;">}</span> <span style="color: #4f76ac;">return</span> <span style="color: #823125;">"not found"</span><span style="color: #1e7c70;">;</span> <span style="color: #1e7c70;">}</span></pre> </li> <li><strong>] call function</strong></li> <ol> <li> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;">var driverIndex = findIndexByKeyValue<span style="color: #1e7c70;">(</span>drivers<span style="color: #1e7c70;">,</span><span style="color: #823125;">"id"</span><span style="color: #1e7c70;">,</span>"48"<span style="color: #1e7c70;">);</span> </pre> </li> </ol></ol> <h2>[REFERENCE]</h2> <ol> <li>] js indexOf arrary,</li> <li>] js indexOf string</li> </ol> <h1 style="text-align: center;"> </h1>