静待花开 2022-05-04 13:56:21
我的核心思想是使用 split
,假设未匹配到返回-1
:
let findIndex = (S, T) => { let index = -1 if (!T.length || T.length > S.length) return index // T 为空字符串或 T 的长度大于 S 均直接返回-1 const arr = S.split(T) if (arr.length > 1) index = arr[0].length return index } findIndex('FishPlusOrange', 'Plus') // 4 findIndex('FishPlusOrange', 'sulP') // -1
需要注意的是,在以上算法中,如果字符串 S 中存在多个字符串 T,仅返回第一个的位置
静待花开 2022-05-04 13:55:21
利用构造函数
function toObject(entry, output = {}, k_name = '') {
for (let key in entry) {
if (entry[key].constructor == Object){
toObject(entry[key], output, k_name ? `${k_name}.${key}` : `${key}`);
}else{
output[`${k_name}.${key}`] = entry[key];
}
}
return output
}
toObject(entry)
- 共 1 页
- 1
在CSS-tricks发现了下面的链接,这个应该算是debounce的根儿了。文中作者给出了一个每行都带注释的版本,有兴趣的小伙伴可以研究下。
Debouncing Javascript Methods
第 3 题:什么是防抖和节流?有什么区别?如何实现?