replace() & replaceAll()

replace():取代(首個)…

//方法會傳回一個新字串,此新字串是透過將原字串與 pattern 比對,以 replacement 取代吻合處而生成

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
const p
const pReplace=p.replace('dog', 'monkey');
console.log('Preplace',pReplace);
//"The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"

replaceAll()取代所有…

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
const  pReplaceAll=p.replaceAll('dog', 'monkey');
console.log('PreplaceAll',pReplaceAll);
//"The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"
const regex = /Dog/ig;
console.log(p.replaceAll(regex, 'ferret'));
//"The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"

splice:從數組中刪除或是取代,數量。

參數(第一個參數,數量,數據)
第一個參數可以是字串,數字,
第二個參數一定是數量:0~…
第三個參數可以是字串,數字:如果有為取代,如果沒有為新增

刪除

const arr =["apple","book","phone"]
const arrSplice=arr.splice("apple",1);
console.log('arrSplice',arrSplice);
//arrSplice ["apple"]
console.log('arr',arr);
//arr ["book","phone"]

取代

const arry =["apple","book","phone"];
const  arrySplice=arry.splice("apple",1,"spack");
console.log('arrySplice',arrySplice);
//"arrySplice" ["apple"]
console.log('arry',arry);
//"arry" ["spack","book","phone"]

新增

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi");
console.log('fruits',fruits);
//"fruits" ["Banana","Orange","Lemon","Kiwi","Apple","Mango"]

every():所有元素符合…

every():所有元素符合…

const people = [
    { name: 'Wes', year: 1988 },
    { name: 'Kait', year: 1986 },
    { name: 'Irv', year: 1970 },
    { name: 'Lux', year: 2015 },
]
const isEvery=people.every((person)=>{
   const currentYear= new Date().getFullYear();
  return currentYear - person.year >= 19;
})
//console.log('是否全部人都超過19歲的人',isEvery)
//false

some():至少一個元素符合...

some():至少一個元素符合…

const people = [
    { name: 'Wes', year: 1988 },
    { name: 'Kait', year: 1986 },
    { name: 'Irv', year: 1970 },
    { name: 'Lux', year: 2015 },
]
const isSome=people.every((person)=>{
   const currentYear= new Date().getFullYear();
  return currentYear - person.year >= 19;
})
//console.log('至少一個19歲的人',isSome)
//true
const array = [1, 2, 3, 4, 5];

// 檢查元素是否為偶數(%2:是否餘2)
const even = (element) => element % 2 === 0;

const someEven=array.some(even);
console.log(someEven);
//true

fill(填充數據,位置開始,結束)

fill(填充數據,位置開始,結束)

const array1 = [1, 2, 3, 4];
//從位置 2 到位置 4 填充 0
console.log(array1.fill(0, 2, 4));
//[1,2,0,0]

//從位置 1 填充 5
console.log(array1.fill(5, 1));
//[1,5,5,5]

//填充6
console.log(array1.fill(6));
// [6, 6, 6, 6]