Some()
- some()์ ๋ฐฐ์ด์ ๊ฐ ์๋ฆฌ๋จผํธ์ ๋ํด์ ํจ์์ ๋ฐํ ๊ฐ์ด ํ๋๋ผ๋ true๊ฐ ์๋์ง ํ์ธํ๋ค
- ํ๋๋ผ๋ true๊ฐ ๋ฐ์ํ๋ฉด true๋ฅผ ๋ฐํํ๋ค
- ๋ชจ๋ false์ธ ๊ฒฝ์ฐ๋ง false๋ฅผ ๋ฐํํ๋ค
/* example */
let testArray = ["walker", "jjh", "tistory"];
const someExample = testArray.some((item) => item == "jjh");
console.log(someExample);
Toggle Function
/* example */
const [checkbox, setCheckbox] = useState([]);
const dataArray = ["walker", "jjh", "tistory"];
const handleCheckbox = (checkValue)=>{
if(checkbox.some((item)=> item === checkValue)){
const removedArray = dataArray.filter((data)=> data !== checkValue);
setCheckbox(removedArray);
}else{
setCheckbox([...checkbox, checkValue]);
}
}
- onPress() ์ด๋ฒคํธ๋ก handleCheckbox ๋ฅผ ๊ฑธ์ด์ค๋ค
- handleCheckbox ์ ํ๋ผ๋ฏธํฐ๋ ์ฒดํฌํ ๊ฐ์ ๋ฐ๋๋ค
- some() ํจ์๋ก ์ฒดํฌํ ๊ฐ์ด checkbox ๋ฐฐ์ด์ ์๋ฆฌ๋จผํธ๋ก ์กด์ฌํ๋์ง ํ์ธํ๋ค
- ์กด์ฌํ๋ค๋ฉด filter() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ checkbox ๋ฐฐ์ด์์ checkValue ๊ฐ์ ์ ์ธํ ๋ฐฐ์ด์ removedArray ์ ๋ฃ์ด์ฃผ๊ณ , setCheckbox(removedArray) ๋ก ์ต์ข
์์ ํ๋ค
- ์กด์ฌํ์ง ์๋๋ค๋ฉด ๊ธฐ์กด checkbox ์ ์๋ฆฌ๋จผํธ๋ค๊ณผ checkValue ๋ก setCheckbox() ๋ฅผ ํ๋ค