{"id":961,"date":"2025-06-12T03:40:05","date_gmt":"2025-06-11T19:40:05","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/laravel\/961.html"},"modified":"2025-06-12T03:40:05","modified_gmt":"2025-06-11T19:40:05","slug":"eloquent-%e6%8c%89%e6%9d%a1%e4%bb%b6%e7%bb%9f%e8%ae%a1%e8%ae%b0%e5%bd%95%ef%bc%88%e5%85%ad%e7%a7%8d%e6%96%b9%e6%b3%95%ef%bc%89","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/961.html","title":{"rendered":"Eloquent: \u6309\u6761\u4ef6\u7edf\u8ba1\u8bb0\u5f55\uff08\u516d\u79cd\u65b9\u6cd5\uff09"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"Bug&#32534;&#35793;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">Bug&#32534;&#35793;&#29422;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22909;&#30340;&#65292;&#35831;&#38382;&#24744;&#24819;&#20102;&#35299;&#21738;&#31181;&#26041;&#27861;&#65311;&#20197;&#19979;&#26159;&#20960;&#31181;&#24120;&#35265;&#30340;&#25353;&#26465;&#20214;&#32479;&#35745;&#35760;&#24405;&#30340;&#26041;&#27861;&#65306;<\/p>\n<h3>&#26041;&#27861;&#19968;&#65306;&#20351;&#29992; <code>where<\/code> &#26041;&#27861;<\/h3>\n<pre><code class=\"language-php\">$users = User::where('age', '&gt;', 18)-&gt;get();<\/code><\/pre>\n<h3>&#26041;&#27861;&#20108;&#65306;&#20351;&#29992; <code>whereBetween<\/code> &#26041;&#27861;<\/h3>\n<pre><code class=\"language-php\">$users = User::whereBetween('age', [20, 30])-&gt;get();<\/code><\/pre>\n<h3>&#26041;&#27861;&#19977;&#65306;&#20351;&#29992; <code>whereIn<\/code> &#26041;&#27861;<\/h3>\n<pre><code class=\"language-php\">$users = User::whereIn('id', [1, 2, 3])-&gt;get();<\/code><\/pre>\n<h3>&#26041;&#27861;&#22235;&#65306;&#20351;&#29992; <code>whereNotIn<\/code> &#26041;&#27861;<\/h3>\n<pre><code class=\"language-php\">$users = User::whereNotIn('id', [1, 2, 3])-&gt;get();<\/code><\/pre>\n<h3>&#26041;&#27861;&#20116;&#65306;&#20351;&#29992; <code>whereHas<\/code> &#26041;&#27861;<\/h3>\n<p>&#20551;&#35774;&#25105;&#20204;&#26377;&#19968;&#20010; <code>posts<\/code> &#34920;&#65292;&#24182;&#19988;&#27599;&#20010;&#29992;&#25143;&#37117;&#26377;&#22810;&#20010;&#24086;&#23376;&#65292;&#25105;&#20204;&#21487;&#20197;&#20351;&#29992; <code>whereHas<\/code> &#26041;&#27861;&#26469;&#32479;&#35745;&#27599;&#20010;&#29992;&#25143;&#30340;&#24086;&#23376;&#25968;&#37327;&#12290;<\/p>\n<pre><code class=\"language-php\">$usersWithPosts = User::withCount('posts')-&gt;get();<\/code><\/pre>\n<h3>&#26041;&#27861;&#20845;&#65306;&#20351;&#29992; <code>groupBy<\/code> &#26041;&#27861;&#21644; <code>count<\/code> &#26041;&#27861;<\/h3>\n<pre><code class=\"language-php\">$usersByAge = User::groupBy('age')-&gt;count();<\/code><\/pre>\n<p>&#36825;&#20123;&#26041;&#27861;&#21487;&#20197;&#26681;&#25454;&#24744;&#30340;&#20855;&#20307;&#38656;&#27714;&#36873;&#25321;&#20351;&#29992;&#12290;&#24076;&#26395;&#36825;&#33021;&#24110;&#21040;&#24744;&#65281;&#22914;&#26524;&#24744;&#26377;&#20219;&#20309;&#20854;&#20182;&#38382;&#39064;&#65292;&#35831;&#38543;&#26102;&#25552;&#38382;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#40657;&#26495;Bug&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#40657;&#26495;Bug&#35762;&#24072;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<body><h2>&#20171;&#32461;<\/h2><p>&#22312;&#20351;&#29992;Laravel&#30340;Eloquent ORM&#26102;&#65292;&#32463;&#24120;&#20250;&#36935;&#21040;&#38656;&#35201;&#26681;&#25454;&#29305;&#23450;&#26465;&#20214;&#26469;&#35745;&#25968;&#25968;&#25454;&#24211;&#34920;&#20013;&#30340;&#35760;&#24405;&#30340;&#24773;&#26223;&#12290;&#36825;&#20351;&#24471;&#24320;&#21457;&#32773;&#33021;&#22815;&#36731;&#26494;&#22320;&#33719;&#21462;&#20851;&#20110;&#25968;&#25454;&#30340;&#20449;&#24687;&#24182;&#22312;&#27492;&#24212;&#29992;&#20013;&#25191;&#34892;&#24517;&#35201;&#30340;&#36923;&#36753;&#12290;&#25105;&#20204;&#23558;&#25506;&#35752;&#22914;&#20309;&#20351;&#29992;Laravel&#30340;Eloquent&#36827;&#34892;&#26465;&#20214;&#35745;&#25968;&#12290;<\/p><h2>&#24320;&#22987;&#20351;&#29992;<\/h2><p>&#22312;&#28145;&#20837;&#25506;&#35752;&#26465;&#20214;&#35745;&#25968;&#20043;&#21069;&#65292;&#35831;&#30830;&#20445;&#24744;&#24050;&#32463;&#35774;&#32622;&#20102;&#19968;&#20010;Laravel&#39033;&#30446;&#24182;&#20351;&#29992;&#20102;Eloquent&#27169;&#22411;&#36827;&#34892;&#25805;&#20316;&#12290;&#22312;&#36825;&#31687;&#25945;&#31243;&#20013;&#65292;&#25105;&#20204;&#23558;&#20351;&#29992;&#19968;&#20010;&#31616;&#21333;&#30340;&#31034;&#20363;&#26469;&#28436;&#31034;&#22914;&#20309;&#23454;&#29616;&#26465;&#20214;&#35745;&#25968;&#12290;<code>Post<\/code>&#27169;&#22411;&#29992;&#20110;&#22312;&#25968;&#25454;&#24211;&#20013;&#34920;&#31034;&#21338;&#23458;&#25991;&#31456;&#12290;<\/p><pre><code>use AppModelsPost;\n\n\/\/ Assuming a Post model already exists\n<\/code><\/pre><p>&#35753;&#25105;&#20204;&#22797;&#20064;&#19968;&#19979;&#22312;Eloquent&#20013;&#26681;&#25454;&#26465;&#20214;&#35745;&#25968;&#35760;&#24405;&#30340;&#20851;&#38190;&#25216;&#24039;&#12290;<\/p><h3>&#22522;&#26412;&#35745;&#25968;<\/h3><p>&#26368;&#31616;&#21333;&#30340;&#35760;&#24405;&#35745;&#25968;&#26041;&#27861;&#23601;&#26159;&#20351;&#29992;&#12290;<code>count()<\/code>&#26041;&#27861;&#12290;<\/p><pre><code>\n\/\/ Count all posts\n$postCount = Post::count();\n<\/code><\/pre><p>&#35753;&#25105;&#20204;&#21482;&#35745;&#25968;&#24050;&#21457;&#24067;&#30340;&#24086;&#23376;&#12290;&#36825;&#38656;&#35201;&#22312;&#26597;&#35810;&#20013;&#28155;&#21152;&#19968;&#20010;&#26465;&#20214;&#12290;<\/p><pre><code>\n\/\/ Count published posts\n$publishedCount = Post::where('status', 'published')-&gt;count();\n<\/code><\/pre><h3>&#26465;&#20214;&#35745;&#25968;&#19982;&#26500;&#24314;&#26041;&#27861;<\/h3><p>&#21487;&#20197;&#20351;&#29992;&#26500;&#24314;&#22120;&#26041;&#27861;&#20026;Eloquent&#26597;&#35810;&#28155;&#21152;&#26465;&#20214;&#65306;<\/p><pre><code>\n\/\/ Count posts with more than 100 likes\n$popularPostsCount = Post::where('likes', '&gt;', 100)-&gt;count();\n<\/code><\/pre><p>&#22312;&#19978;&#36848;&#26597;&#35810;&#20013;&#65292;<code>where()<\/code>&#23427;&#29992;&#20110;&#28155;&#21152;&ldquo;&#22823;&#20110;&rdquo;&#26465;&#20214;&#12290;<\/p><h3>&#35745;&#25968;&#30456;&#20851;&#35760;&#24405;<\/h3><p>&#24456;&#22810;&#26102;&#20505;&#65292;&#25105;&#20204;&#38656;&#35201;&#23545;&#19982;&#21478;&#19968;&#20010;&#27169;&#22411;&#20855;&#26377;&#26576;&#31181;&#20851;&#31995;&#30340;&#35760;&#24405;&#36827;&#34892;&#35745;&#25968;&#12290;&#35753;&#25105;&#20204;&#20351;&#29992;<code>Comment<\/code>&#20316;&#20026;&#30456;&#20851;&#27169;&#22411;&#65306;<\/p><pre><code>\n$commentsCount = Post::withCount('comments')-&gt;get();\n$commentsCount = $commentsCount-&gt;map(function ($item) {\n    return $item-&gt;comments_count;\n});\n<\/code><\/pre><p>&#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#22826;&#26126;&#30333;&#20320;&#30340;&#24847;&#24605;&#65292;&#35831;&#37325;&#26032;&#25551;&#36848;&#19968;&#19979;&#12290;<code>withCount()<\/code>&#26041;&#27861;&#23558;&#20250;&#28155;&#21152;&#19968;&#20010;&ldquo;&rdquo;<code>comments_count<\/code>&#20026;&#27599;&#20010;&#20174;Post&#27169;&#22411;&#33719;&#21462;&#30340;&#23454;&#20363;&#28155;&#21152;&#23646;&#24615;&#12290;<\/p><p>&#20320;&#21487;&#20197;&#38468;&#21152;&#26465;&#20214;&#21040;<code>withCount()<\/code>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<\/p><pre><code>\n\/\/ Count comments that are flagged\n$postsWithFlaggedComments = Post::withCount(['comments' =&gt; function ($query) {\n    $query-&gt;where('flagged', true);\n}])-&gt;get();\n<\/code><\/pre><h3>&#21160;&#24577;&#35775;&#38382;&#32858;&#21512;&#23646;&#24615;<\/h3><p>&#20351;&#29992;Eloquent&#65292;&#21487;&#20197;&#21160;&#24577;&#22320;&#36890;&#36807;&#34394;&#25311;&#23646;&#24615;&#26469;&#35745;&#25968;&#35760;&#24405;&#65306;<\/p><pre><code>\n\/\/ Eloquent Model - Dynamic properties\n\n$posts = Post::all();\n\nforeach ($posts as $post) {\n    echo $post-&gt;where('status', 'published')-&gt;count();\n}\n<\/code><\/pre><h3>&#20851;&#31995;&#32858;&#21512;<\/h3><p>&#22312;&#22788;&#29702;&#20851;&#31995;&#26102;&#65292;&#23545;&#32858;&#21512;&#36827;&#34892;&#38468;&#21152;&#26465;&#20214;&#21487;&#20197;&#38750;&#24120;&#26377;&#24110;&#21161;&#65306;<\/p><pre><code>\n\/\/ Count of related comments that are not flagged\n$postWithNonFlaggedComments = Post::withCount(['comments' =&gt; function ($query) {\n    $query-&gt;where('flagged', false);\n}])-&gt;get();\n<\/code><\/pre><p>&#24456;&#37325;&#35201;&#30340;&#26159;&#19981;&#35201;&#25226;&#23427;&#20204;&#28151;&#28102;&#20102;&#12290;<code>withCount()<\/code>&#26041;&#27861;&#26159;&#12290;<code>loadCount()<\/code>&#22312;&#29616;&#26377;&#30340;&#27169;&#22411;&#23454;&#20363;&#19978; eager &#21152;&#36733;&#20851;&#31995;&#35745;&#25968;&#12290;<\/p><p>&#22522;&#20110;&#35745;&#25968;&#30340;&#19981;&#21516;&#65292;&#24744;&#21487;&#33021;&#38656;&#35201;&#37319;&#21462;&#19981;&#21516;&#30340;&#34892;&#21160;&#12290;<\/p><pre><code>\n$post = Post::find(1);\n\nif ($post-&gt;comments()-&gt;where('flagged', true)-&gt;count() &gt; 5) {\n    \/\/ Take action if there are more than 5 flagged comments on the post\n}\n<\/code><\/pre><h3>&#39640;&#32423;&#29992;&#27861;<\/h3><p>&#23545;&#20110;&#39640;&#32423;&#20351;&#29992;&#65292;Eloquent&#20801;&#35768;&#25353;&#29305;&#23450;&#23383;&#27573;&#23545;&#32479;&#35745;&#25968;&#25454;&#36827;&#34892;&#20998;&#32452;&#12290;&#20363;&#22914;&#65306;<\/p><pre><code>\n$postsByYear = Post::query()\n    -&gt;selectRaw('YEAR(created_at) as year')\n    -&gt;selectRaw('COUNT(*) as total')\n    -&gt;groupBy('year')\n    -&gt;get();\n<\/code><\/pre><p>&#22312;&#19978;&#36848;&#31034;&#20363;&#20013;&#65292;&#25105;&#20204;&#25353;&#24180;&#20221;&#23545;&#24086;&#23376;&#36827;&#34892;&#20998;&#32452;&#24182;&#35745;&#25968;&#12290;<\/p><h2>&#32467;&#35770;<\/h2><p>&#26412;&#25945;&#31243;&#20171;&#32461;&#20102;&#20351;&#29992;Eloquent&#22312;Laravel&#24212;&#29992;&#31243;&#24207;&#20013;&#26681;&#25454;&#19981;&#21516;&#26465;&#20214;&#35745;&#25968;&#35760;&#24405;&#30340;&#26041;&#27861;&#12290;&#29702;&#35299;&#22914;&#20309;&#26377;&#25928;&#22320;&#24212;&#29992;&#36825;&#20123;&#26041;&#27861;&#23545;&#20110;&#26500;&#24314;&#39640;&#25928;&#22788;&#29702;&#25968;&#25454;&#25805;&#20316;&#30340;Laravel&#24212;&#29992;&#33267;&#20851;&#37325;&#35201;&#12290;<\/p><p>&#20351;&#29992;&#36825;&#20123;&#26041;&#27861;&#26102;&#38656;&#35201;&#35880;&#24910;&#65292;&#32771;&#34385;&#21040;&#22312;&#22788;&#29702;&#22823;&#25968;&#25454;&#38598;&#26102;&#24212;&#29992;&#31243;&#24207;&#30340;&#24615;&#33021;&#34920;&#29616;&#12290;&#21033;&#29992;&#25968;&#25454;&#24211;&#32034;&#24341;&#21644;&#20248;&#21270;&#30340;SQL&#26597;&#35810;&#23545;&#20110;&#32500;&#25345;&#39640;&#24615;&#33021;&#30340;&#24212;&#29992;&#31243;&#24207;&#33267;&#20851;&#37325;&#35201;&#12290;<\/p><p>&#36890;&#36807;&#25484;&#25569;Eloquent&#65292;&#24320;&#21457;&#32773;&#21487;&#20197;&#20197;&#26356;&#26131;&#20110;&#38405;&#35835;&#30340;&#26041;&#24335;&#26500;&#24314;&#22797;&#26434;&#30340;&#26597;&#35810;&#65292;&#30456;&#27604;&#20351;&#29992;&#21407;&#22987;SQL&#65292;&#36825;&#19981;&#20165;&#25552;&#39640;&#20102;&#24320;&#21457;&#25928;&#29575;&#65292;&#20063;&#35753;&#25972;&#20010;&#24320;&#21457;&#36807;&#31243;&#21464;&#24471;&#26356;&#21152;&#24841;&#24555;&#12290;<\/p><\/body>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--3\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/xygcfznnzczhsdmwydzhsmzzzmddnq-400x300.jpg\" alt=\"&#38271;&#24037;&#30721;&#22900;1523\" class=\"wp-image-2906\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#38271;&#24037;&#30721;&#22900;1523<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>Eloquent JavaScript &#26159;&#19968;&#31181;&#38750;&#24120;&#27969;&#34892;&#30340;&#32534;&#31243;&#35821;&#35328;&#65292;&#23427;&#20801;&#35768;&#25105;&#20204;&#32534;&#20889;&#26356;&#31616;&#27905;&#21644;&#26131;&#20110;&#29702;&#35299;&#30340;&#20195;&#30721;&#12290;&#22312; Eloquent &#20013;&#65292;&#25105;&#20204;&#21487;&#20197;&#20351;&#29992; <code>map()<\/code> &#26041;&#27861;&#26469;&#25353;&#29305;&#23450;&#26465;&#20214;&#23545;&#25968;&#25454;&#36827;&#34892;&#31579;&#36873;&#21644;&#25490;&#24207;&#12290;&#19979;&#38754;&#25105;&#23558;&#20171;&#32461;&#20845;&#31181;&#19981;&#21516;&#30340;&#26041;&#27861;&#26469;&#23454;&#29616;&#36825;&#19968;&#21151;&#33021;&#12290;<\/p>\n<h3>1. &#20351;&#29992; <code>filter()<\/code><\/h3>\n<p><code>filter()<\/code> &#26041;&#27861;&#29992;&#20110;&#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#25968;&#32452;&#65292;&#20854;&#20013;&#21482;&#21253;&#21547;&#37027;&#20123;&#28385;&#36275;&#25351;&#23450;&#26465;&#20214;&#30340;&#23545;&#35937;&#25110;&#20540;&#12290;<\/p>\n<pre><code class=\"language-javascript\">const records = [\n  { id: 1, name: 'Alice', age: 25 },\n  { id: 2, name: 'Bob', age: 30 },\n  { id: 3, name: 'Charlie', age: 40 }\n];\n\n\/\/ &#25353;&#24180;&#40836;&#22823;&#20110;28&#23681;&#30340;&#35760;&#24405;&#36807;&#28388;\nconst filteredRecords = records.filter(record =&gt; record.age &gt; 28);\nconsole.log(filteredRecords); \/\/ &#36755;&#20986;&#65306;[ { id: 2, name: 'Bob', age: 30 }, { id: 3, name: 'Charlie', age: 40 } ]<\/code><\/pre>\n<h3>2. &#20351;&#29992; <code>reduce()<\/code><\/h3>\n<p><code>reduce()<\/code> &#26041;&#27861;&#21487;&#20197;&#36941;&#21382;&#25968;&#32452;&#24182;&#25191;&#34892;&#19968;&#31995;&#21015;&#20989;&#25968;&#65292;&#20174;&#32780;&#29983;&#25104;&#19968;&#20010;&#21333;&#20010;&#32467;&#26524;&#12290;&#36825;&#37324;&#65292;&#25105;&#20204;&#20351;&#29992; <code>reduce()<\/code> &#26469;&#35745;&#31639;&#25152;&#26377;&#23545;&#35937;&#20013;&#30340; <code>age<\/code> &#23646;&#24615;&#20043;&#21644;&#12290;<\/p>\n<pre><code class=\"language-javascript\">const records = [\n  { id: 1, name: 'Alice', age: 25 },\n  { id: 2, name: 'Bob', age: 30 },\n  { id: 3, name: 'Charlie', age: 40 }\n];\n\nlet totalAge = records.reduce((acc, record) =&gt; acc + record.age, 0);\n\nconsole.log(totalAge); \/\/ &#36755;&#20986;&#65306;95<\/code><\/pre>\n<h3>3. &#20351;&#29992; <code>map()<\/code> &#21644; <code>reduce()<\/code> &#32467;&#21512;<\/h3>\n<p>&#22914;&#26524;&#20320;&#24076;&#26395;&#25353;&#24180;&#40836;&#20998;&#32452;&#65292;&#24182;&#19988;&#38656;&#35201;&#21516;&#26102;&#33719;&#21462;&#27599;&#20010;&#32452;&#30340;&#24635;&#24180;&#40836;&#65292;&#20320;&#21487;&#20197;&#32467;&#21512; <code>map()<\/code> &#21644; <code>reduce()<\/code>&#12290;<\/p>\n<pre><code class=\"language-javascript\">const records = [\n  { id: 1, name: 'Alice', age: 25 },\n  { id: 2, name: 'Bob', age: 30 },\n  { id: 3, name: 'Charlie', age: 40 }\n];\n\nconst groupedRecords = records.map(record =&gt; ({\n  group: record.age,\n  count: records.filter(r =&gt; r.age === record.age).length,\n  totalAge: records.filter(r =&gt; r.age === record.age).reduce((acc, r) =&gt; acc + r.age, 0)\n}));\n\ngroupedRecords.forEach(group =&gt; console.log(`${group.group}: ${group.count} people with an average age of ${Math.round(group.totalAge \/ group.count)} years`));<\/code><\/pre>\n<h3>4. &#20351;&#29992; <code>groupBy()<\/code> &#21644; <code>reduce()<\/code> &#32467;&#21512;<\/h3>\n<p>&#22914;&#26524;&#20320;&#24819;&#35201;&#25353;&#24180;&#40836;&#20998;&#32452;&#65292;&#28982;&#21518;&#23545;&#27599;&#32452;&#36827;&#34892;&#19968;&#20123;&#25805;&#20316;&#65292;&#27604;&#22914;&#27714;&#24179;&#22343;&#24180;&#40836;&#65292;&#20320;&#21487;&#20197;&#20808;&#20351;&#29992; <code>groupBy()<\/code> &#20998;&#32452;&#65292;&#28982;&#21518;&#20877;&#29992; <code>reduce()<\/code> &#36827;&#34892;&#25805;&#20316;&#12290;<\/p>\n<pre><code class=\"language-javascript\">const records = [\n  { id: 1, name: 'Alice', age: 25 },\n  { id: 2, name: 'Bob', age: 30 },\n  { id: 3, name: 'Charlie', age: 40 }\n];\n\nconst groupedRecords = records.reduce((acc, record) =&gt; {\n  const group = acc.find(group =&gt; group.key === record.age);\n  if (!group) {\n    group = { key: record.age };\n    acc.push(group);\n  }\n\n  return acc;\n}, []);\n\ngroupedRecords.forEach(group =&gt; {\n  console.log(`Group ${group.key}:`);\n  group.value.forEach(record =&gt; console.log(`${record.name}: ${record.age} years old`));\n});<\/code><\/pre>\n<h3>5. &#20351;&#29992; <code>findIndex()<\/code> &#21644; <code>reduce()<\/code> &#32467;&#21512;<\/h3>\n<p>&#22914;&#26524;&#20320;&#24819;&#25214;&#21040;&#31532;&#19968;&#20010;&#28385;&#36275;&#29305;&#23450;&#26465;&#20214;&#30340;&#23545;&#35937;&#65292;&#21487;&#20197;&#20351;&#29992; <code>findIndex()<\/code> &#21644; <code>reduce()<\/code>&#12290;<\/p>\n<pre><code class=\"language-javascript\">const records = [\n  { id: 1, name: 'Alice', age: 25 },\n  { id: 2, name: 'Bob', age: 30 },\n  { id: 3, name: 'Charlie', age: 40 }\n];\n\nconst foundRecord = records.reduce((acc, record, index) =&gt; {\n  if (record.age &gt; 28 &amp;&amp; record.id &gt; acc.id) {\n    return record;\n  }\n\n  return acc;\n}, {});\n\nconsole.log(foundRecord); \/\/ &#36755;&#20986;&#65306;{ id: 2, name: 'Bob', age: 30 }<\/code><\/pre>\n<h3>6. &#20351;&#29992; <code>every()<\/code> &#21644; <code>reduce()<\/code> &#32467;&#21512;<\/h3>\n<p>&#22914;&#26524;&#35201;&#26816;&#26597;&#25152;&#26377;&#30340;&#23545;&#35937;&#26159;&#21542;&#37117;&#31526;&#21512;&#26576;&#20010;&#26465;&#20214;&#65292;&#21487;&#20197;&#20351;&#29992; <code>every()<\/code> &#21644; <code>reduce()<\/code>&#12290;<\/p>\n<pre><code class=\"language-javascript\">const records = [\n  { id: 1, name: 'Alice', age: 25 },\n  { id: 2, name: 'Bob', age: 30 },\n  { id: 3, name: 'Charlie', age: 40 }\n];\n\nconst allRecordsMeetCriteria = records.every(record =&gt; record.age &gt; 28 &amp;&amp; record.id &lt; 40);\n\nconsole.log(allRecordsMeetCriteria); \/\/ &#36755;&#20986;&#65306;true<\/code><\/pre>\n<p>&#20197;&#19978;&#23601;&#26159;&#25353;&#26465;&#20214;&#32479;&#35745;&#35760;&#24405;&#30340;&#20845;&#31181;&#19981;&#21516;&#26041;&#27861;&#65292;&#20320;&#21487;&#20197;&#26681;&#25454;&#20855;&#20307;&#38656;&#27714;&#36873;&#25321;&#21512;&#36866;&#30340;&#26041;&#27861;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#22909;&#30340;&#65292;&#35831;&#38382;&#24744;&#24819;&#20102;&#35299;&#21738;&#31181;&#26041;&#27861;&#65311;&#20197;&#19979;&#038;#26..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[44],"tuisongtax":[],"class_list":["post-961","my1js","type-my1js","status-publish","hentry","my1js2nav-laravel"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=961"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=961"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=961"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}