發表文章

目前顯示的是 11月, 2020的文章

[JS] 搜尋物件中所有值

 解法原文 https://stackoverflow.com/questions/8517089/js-search-in-object-values/50181192 function search ( arr, s ) { var matches = [], i, key; for ( i = arr.length; i--; ) for ( key in arr[i] ) if ( arr[i].hasOwnProperty(key) && arr[i][key].indexOf(s) > -1 ) matches.push( arr[i] ); // <-- This can be changed to anything return matches; }; // dummy data var items = [ { "foo" : "bar" , "bar" : "sit" }, { "foo" : "lorem" , "bar" : "ipsum" }, { "foo" : "dolor" , "bar" : "amet" } ]; var result = search(items, 'lo' ); // search "items" for a query value console .log(result); // print the result ====================== 改成TS寫法,並且將大小寫也能搜尋到 arr為要傳入的arry object s為要搜尋的值    function   search...