# 从一组数据中找到指定的单条数据的方法

## 标准答案,find方法

<code class="language-javascript hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> json = [{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"张三"</span>},{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"李四"</span>},{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"王五"</span>}];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

<code class="language-javascript hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> data = json.find(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(e)</span>{</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> e.id == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>});
console.log(data);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

## 我的方案,for循环

<code class="language-javascript hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> json = [{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"张三"</span>},{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"李四"</span>},{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"王五"</span>}];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> data = getJsonById(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,json);
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">getJsonById</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id,data)</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < data.length; i++) {
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (data[i].id==id) {
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> data[i];
}
};

06-01 4983

03-04 776

11-13 1011

08-29 2511

04-14 55

09-30 2396

09-08 1430

05-16 215

03-07 237

12-12 5523