{"id":669,"date":"2023-03-01T20:34:33","date_gmt":"2023-03-01T20:34:33","guid":{"rendered":"https:\/\/freelance.in.th\/?p=669"},"modified":"2023-03-09T15:23:23","modified_gmt":"2023-03-09T15:23:23","slug":"javascript-how-to-find-data-in-an-array-objects","status":"publish","type":"post","link":"https:\/\/freelance.in.th\/index.php\/2023\/03\/01\/javascript-how-to-find-data-in-an-array-objects\/","title":{"rendered":"JavaScript : How to find data in an array objects."},"content":{"rendered":"\n<p>To find objects in an array that have a specific value for the <code>docNo<\/code> property, you can use the <code><strong><em>filter()<\/em><\/strong><\/code> method. Here&#8217;s an example code snippet that filters the <code>data<\/code> array to find all objects that have a <code><strong>docNo<\/strong><\/code> value of <code><strong>\"doc123\"<\/strong><\/code>:<\/p>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-1 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-columns is-not-stacked-on-mobile is-layout-flex wp-container-core-columns-is-layout-1 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<a href=\"https:\/\/clicks.pipaffiliates.com\/c?m=77537&#038;c=711675\" referrerpolicy=\"no-referrer-when-downgrade\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ads.pipaffiliates.com\/i\/77537?c=711675\" width=\"120\" height=\"600\" referrerpolicy=\"no-referrer-when-downgrade\"\/><\/a>\n<\/div>\n<\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>const data = &#91;\n  {\n    \"id\": 1,\n    \"docNo\": \"doc123\",\n  },\n  {\n    \"id\": 2,\n    \"docNo\": \"doc456\",\n  },\n  {\n    \"id\": 3,\n    \"docNo\": \"doc123\",\n  }\n];\n\nconst doc123Objects = data.filter(obj =&gt; obj.docNo === \"doc123\");\n\nconsole.log(doc123Objects); \/\/ Output: &#91; { \"id\": 1, \"docNo\": \"doc123\", ... }, { \"id\": 3, \"docNo\": \"doc123\", ... } ]\n<\/code><\/pre>\n<\/div>\n\n\n\n<p>In this example, the<strong><em> <code>filter()<\/code><\/em><\/strong> method is called on the <code>data<\/code> array and returns a new array that contains only the objects with a <code>docNo<\/code> property of <code><strong>\"doc123\"<\/strong><\/code>. The filtered array is assigned to the <code><strong>doc123Objects<\/strong><\/code> variable and then logged to the console.<\/p>\n\n\n\n<p>If you only want to find the first object that has a <code>docNo<\/code> value of <code><strong>\"doc123\"<\/strong><\/code>, you can use the <code><strong><em>find()<\/em><\/strong><\/code> method instead of <code><strong><em>filter()<\/em><\/strong><\/code>. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const doc123Object = data.find(obj =&gt; obj.docNo === \"doc123\");\n\nconsole.log(doc123Object); \/\/ Output: { \"id\": 1, \"docNo\": \"doc123\", ... }\n<\/code><\/pre>\n\n\n\n<a href=\"https:\/\/clicks.pipaffiliates.com\/c?m=71492&#038;c=711675\" referrerpolicy=\"no-referrer-when-downgrade\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ads.pipaffiliates.com\/i\/71492?c=711675\" width=\"600\" height=\"90\" referrerpolicy=\"no-referrer-when-downgrade\"\/><\/a>\n","protected":false},"excerpt":{"rendered":"<p>To find objects in an array that have a specific value for the docNo property, you can use the filter() method. Here&#8217;s an example code snippet that filters the data array to find all objects that have a docNo value of &#8220;doc123&#8221;: In this example, the filter() method is called on the data array and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[43],"class_list":["post-669","post","type-post","status-publish","format-standard","hentry","category-programming","tag-javascript"],"_links":{"self":[{"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts\/669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/comments?post=669"}],"version-history":[{"count":2,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts\/669\/revisions"}],"predecessor-version":[{"id":735,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts\/669\/revisions\/735"}],"wp:attachment":[{"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/media?parent=669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/categories?post=669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/tags?post=669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}