關於 String.repleace()
前言
內容
const p = 'Dog_dog_Dog'; console.log(p.replace( /Dog/i , 'Cat'));//"Cat_dog_Dog" console.log(p.replaceAll( /Dog/ig , 'Cat'));//"Cat_Cat_Cat"
const p = 'Dog_dog_Dog'; console.log(p.replace( /Dog/i , 'Cat'));//"Cat_dog_Dog" console.log(p.replaceAll( /Dog/ig , 'Cat'));//"Cat_Cat_Cat"
在先前的 關於 JavaScript 的 使用負值當索引的注意事項 提過 Array.at() ,今天發現 String 也可以使用,在此把學習的過程做個紀錄。
範例如下
let str = 'abcdefg'; console.log( str.at(-2) );// "f" let str1 = str.slice(3,5); console.log(str1);// "de" //Follow code has trap... let str2 = str.slice(-2,0); console.log(str2);// "" // let str3 = str.slice(-2); console.log(str3);// "fg" //
用法跟 Array.at() 一樣,只是這次是對 String 來操作,跟 Array 一樣,在 Slice() 的時候會有不太直覺的操作,就像範例的"str2",幸運的是它和 Array 的行為是一致的,只要把字串想成是字元的陣列就可以接受了。
[ developer.mozilla.org ] String.prototype.at()
最近發現 String 可以直接搜尋字串,在此做個紀錄。
範例如下
const str = 'Blue Whale'; console.log( str.indexOf('Blue') ); console.log( str.indexOf('Blute') ); console.log( str.indexOf('Whale', 0) ); console.log( str.indexOf('Whale', 5) ); console.log( str.indexOf('Whale', 7) ); // 0 // -1 // 5 // 5 // -1
使用的方法很簡單,只需把要找的字串當引數即可,如範例的第一次與第二次的使用,回傳的結果是索引值,如果找不到會回傳"-1",第三次以後會輸入第二個引數,這第二個引數是指定從那個索引值開始找,可以把不輸入引數就是第二個引數輸入 0 的結果。
最近看到 Array.every() ,抽空做個學習,在此做個紀錄。
範例如下
const isBelowThreshold = function(currentValue) { //console.log(currentValue); return currentValue < 30 }; const array1 = [1, 30, 39, 49, 10, 13]; console.log(array1.every(isBelowThreshold)); // console.log(array1.find( (element)=>element >= 30) === undefined );
Array.every() 的功能是用來對陣列的每個元素做檢查用的,檢查使用的是個 funciton ,必須還傳 true 或 false ,接著就透過這個 function 來對每個元素做檢查,在"isBlowThreshold"裡使用 console.log() 可以發現會從頭開始檢查,當回傳是 false 的時候就會終止,這讓我想到最近學的 Array.find() ,所以範例用 Array.find() 也實現了一樣的效果,要注意的是檢查的條件也一點點改變。這個功能幾乎可以被 Array.find() 給替代,而且 Array.every() 只支援從頭開始檢查,而 Array.find() 卻可以透過 Array.findLast() 來從尾開始檢查,所以可以的話就用 Array.find() 來替代吧。
[ developer.mozilla.org ] Array.prototype.every()
最近看到 Array.flat() ,抽個空作個學習,在此做個紀錄。
範例如下
const arr1 = [0, 1, 2, [3, 4] ]; console.log(arr1.flat()); const arr2 = [5, 6, [7], [[[8, 9]]]]; console.log(arr2.flat(3)); // [0, 1, 2, 3, 4] // [5, 6, 7, 8, 9]
Array.flat() 的功能是擺陣列裡的陣列展開,可以看到範例的第一次使用後,後方的陣列被"展開"了,第二次使用時有輸入一個引數,這個引數是用來設定最多展開的次數,所以後方的"[[[8,9]]]"就會直接被展開,也可以把ˇ不輸入引數時想成"flat(1)"。
最近發現 ECMA Script 的 2023 的 新標準新增的 findLast() 與 findLastIndex() ,抽空做個學習,在此做個紀錄。
在先前的標準就有 find() 與 findIndex() ,用途是找尋陣列裡的元素,可以透過引數來決定尋找的條件, find() 找的是內容 , findIndex() 找的是索引值,範例如下
const array1 = [5, 12, 130, 70, 44]; // const found = array1.find((element) => element > 45); console.log(`findValue:${found}`); // const foundIndex = array1.findIndex((element) => element > 45); console.log(`findIndex:${foundIndex}`); // const foundLast = array1.findLast((element) => element > 45); console.log(`findLastValue:${foundLast}`); // const foundLastIndex = array1.findLastIndex((element) => element > 45); console.log(`findLastIndex:${foundLastIndex}`); // "findValue:130" // "findIndex:2" // "findLastValue:70" // "findLastIndex:3"
範例示範了用同一個條件對同一個陣列做找尋的結果, findLast() 與 findLastIndex() 就如同字面上的意思是從陣列的最後開始找,所以 find() 的結果是 130 , findLast() 的結果是 70 ,而 findLastIndex() 就是 findLast() 回傳索引值的版本。
最近發現 Promise 還有個 any() ,抽個空作個學習,在此做個紀錄。
範例如下
function promise(num, time = 50) { return new Promise((resolve, reject) => { setTimeout(() => { console.log(`num:${ num } is done`); (num>0) ? resolve(`value:${num}, 成功`) : reject(`value:${num},失敗`); }, time); }); } Promise.any([promise(0,5000), promise(2,500), promise(3, 1000)]) .then(res => { console.log(res); }) .catch(res =>{ console.log(res); }) .finally(() => { console.log(`It's done`); console.log(`finally block `); }); // "num:2 is done" // "value:2, 成功" // "It's done" // "finally block " // "num:3 is done" // "num:0 is done"
Promise.any() 一樣是一次啟動多個 Promise ,但跟之前的 Promise.all() 或 Promise.allSettled() 不一樣的是它只要其中一個 Promise 進入 resolve() 或 reject() 就開始喚起 then() 或 catch() ,範例的" num:2" 會最快完成,所以 500ms 就進入了 then() ,之後就 finally() ,但可以看到跑完 finally() 後," num:0" 與 " num:3" 還是會繼續跑,但不會進到任何一個 then() 或 catch()。