0
我有以下代碼:搜索使用ramda函數對象
const trimString = (s) => s.trim()
const compareObjects = (o1, o2) => equals(o1, o2)
const itemExists = (haystack, needle) => contains(haystack, needle)
const objects = [
{ duration: '0.360000', name: 'Arthur', time: '0.660000', paragraph: 'p0-0' },
{ duration: '0.150000', name: 'the', time: '1.020000', paragraph: 'p0-0' },
{ duration: '0.380000', name: 'rat', time: '1.170000', paragraph: 'p0-0' },
{ duration: '0.770000', name: '.', time: '1.550000', paragraph: 'p0-0' },
{ duration: '0.360000', name: 'Arthur', time: '89.820000', paragraph: 'p1-0' },
{ duration: '0.390000', name: 'stood', time: '90.180000', paragraph: 'p1-0' },
{ duration: '0.090000', name: 'and', time: '90.570000', paragraph: 'p1-0' }
];
function searchFor(toSearch) {
var results = [];
toSearch = trimString(toSearch); // trim it
for(var i=0; i<objects.length; i++) {
for(var key in objects[i]) {
if(objects[i][key].indexOf(toSearch)!=-1) {
if(!itemExists(results, objects[i])) results.push(objects[i]);
}
}
}
return results;
}
log(searchFor('Arthur'))
我得到的結果是:
[
{
duration: '0.360000',
name: 'Arthur',
time: '0.660000',
paragraph: 'p0-0'
},
{
duration: '0.360000',
name: 'Arthur',
time: '89.820000',
paragraph: 'p1-0'
}
]
但我被困在如何使之成爲一個ramda功能,並能夠將這種添加多個搜索項並僅顯示結果p0-0
如果我使用Arthur the
任何建議是非常感謝。
對我的要求有點不清楚。你真的想搜索對象的*每個*屬性嗎?也就是說,即使'{... time:'1.550000',...}'是唯一一個名字中帶有'。'的字符,如果您搜索了''。',您是否希望檢索自每個時間段和時間以來,每一條記錄都包含它 –
然後也不清楚「亞瑟鼠」是否會匹配你的第一段,或只有連續的詞,如「亞瑟」,「亞瑟」,「亞瑟鼠」或「老鼠」 –
此外,它看起來像儘管['String.prototype.trim'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)會替代你'trimString'函數和[ 'R.equals'](http://ramdajs.com/docs/#equals)儘管有些更強大,但可能會用於您的'compareObjects',給定您的數據。 –