<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anjan</title>
    <description>The latest articles on DEV Community by Anjan (@anjan).</description>
    <link>https://dev.to/anjan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F129246%2Fd1457d1e-b2cb-43d1-a52f-b03209024840.png</url>
      <title>DEV Community: Anjan</title>
      <link>https://dev.to/anjan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anjan"/>
    <language>en</language>
    <item>
      <title>How to get index from a JSON object with value: javascript</title>
      <dc:creator>Anjan</dc:creator>
      <pubDate>Sun, 20 Jan 2019 18:41:06 +0000</pubDate>
      <link>https://dev.to/anjan/how-to-get-index-from-a-json-object-with-value-javascript-35h1</link>
      <guid>https://dev.to/anjan/how-to-get-index-from-a-json-object-with-value-javascript-35h1</guid>
      <description>

&lt;p&gt;In modern browsers you can use findIndex:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var students = [
 {id: 100 },
 {id: 200},
 {id: 300},
 {id: 400},
 {id: 500}
];
var index = students.findIndex(std=&amp;gt; std.id === 200);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But this function is not supported by even not so old versions of a few browser as well as in IE (EDGE supports it). So below is a workaround using &lt;a href="https://anjandutta.com/javascript-es6-default-parameter/"&gt;javascript&lt;/a&gt;:&lt;br&gt;
You can use either &lt;code&gt;Array.forEach&lt;/code&gt; or &lt;code&gt;Array.find&lt;/code&gt; or &lt;code&gt;Array.filter&lt;/code&gt;&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var students = [
 {id: 100 },
 {id: 200},
 {id: 300},
 {id: 400},
 {id: 500}
];
var index = -1;
var needle = 200;
var filteredRes = students.find(function(item, i){
 if(item.id === needle){
 index = i;
 return i;
 }
});
console.log(index, filteredRes);
/*Result: 1 Object { id: 200 }*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This method takes little more overhead as it loops through the whole object to search for the match. So, for lengthy JSON data, this method is not suggested(even though it gets the work done).&lt;/p&gt;


</description>
      <category>javascript</category>
      <category>findindex</category>
      <category>objectindex</category>
    </item>
  </channel>
</rss>
