🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# js实现本地搜索 localSearch(e){ let keyword = e.detail.value console.log("关键词",keyword) if(e.detail.value != ""){ // 进行搜索 let arrnew = this.DataList.map((item,index)=>{ return Object.assign({},{ "id":item.id, 'name':item.name }) }) console.log("复制的数组",arrnew) ``` <pre class="calibre14">``` <span class="token2">let</span> newData <span class="token">=</span> arrnew<span class="token1">.</span><span class="token3">filter</span><span class="token1">(</span><span class="token1">(</span>item<span class="token">=></span><span class="token1">{</span> <span class="token2">if</span><span class="token1">(</span>item<span class="token1">.</span>name<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span>keyword<span class="token1">)</span><span class="token">></span><span class="token">-</span><span class="token5">1</span><span class="token1">)</span><span class="token1">{</span> <span class="token2">return</span> item <span class="token1">}</span> <span class="token2">return</span> newData <span class="token1">}</span><span class="token1">)</span><span class="token1">)</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span class="token4">"结果"</span><span class="token1">,</span>newData<span class="token1">)</span> <span class="token2">if</span><span class="token1">(</span>newData<span class="token1">.</span>length <span class="token">></span> <span class="token5">0</span><span class="token1">)</span><span class="token1">{</span> <span class="token2">this</span><span class="token1">.</span>selectList <span class="token">=</span> newData <span class="token2">this</span><span class="token1">.</span>$<span class="token2">set</span><span class="token1">(</span><span class="token2">this</span><span class="token1">.</span>btnText<span class="token1">,</span><span class="token4">"btnText"</span><span class="token1">,</span><span class="token4">"X"</span><span class="token1">)</span> <span class="token1">}</span><span class="token2">else</span><span class="token1">{</span> <span class="token2">this</span><span class="token1">.</span>$<span class="token2">set</span><span class="token1">(</span><span class="token2">this</span><span class="token1">.</span>btnText<span class="token1">,</span><span class="token4">"btnText"</span><span class="token1">,</span><span class="token4">"增加"</span><span class="token1">)</span> <span class="token2">this</span><span class="token1">.</span>selectList <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span> <span class="token2">this</span><span class="token1">.</span>$u<span class="token1">.</span><span class="token3">toast</span><span class="token1">(</span><span class="token4">"暂无数据"</span><span class="token1">)</span> <span class="token1">}</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>newData<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">,</span> ``` ```