1. ์ปดํฌ๋ํธ ๋ฐ๋ณต Iteration
๐ ๋ฆฌ์กํธ์์์ ๋ฐ๋ณต
- ๋ชฉ๋ก ์์๋ค์ ๋ฐ๋ณต์ฒ๋ฆฌ ํ ๋๋ mapํจ์๋ฅผ ์ด์ฉ ํฉ๋๋ค.
- ๋ฐ๋ณต ์ปดํฌ๋ํธ์๋ ๋ฐ๋์ key props๋ฅผ ์ ๋ฌํด์ผ ํฉ๋๋ค.
๐บ ๋ฐฐ์ด๊ณผ ๊ด๋ จ๋ map๊ณผ filter ๋ฉ์๋์ ๋ํด์ ์์๋ณด์.
๐ map ํจ์๋ ์คํํ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ง๊ณ , ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค ๋ ์ฌ์ฉํ๋ค.
array.map(callbackFunction(currenValue, index, array), thisArg)
- currenValue: ํ์ฌ๊ฐ
- index: ํ์ฌ์ธ๋ฑ์ค
- array: ํ์ฌ๋ฐฐ์ด,
- thisArg: callbackFunction ๋ด์์ this๋ก ์ฌ์ฉ๋ ๊ฐ
const IterationComponent = () => {
const arr = [1,2,3,4,5];
const newArr = arr.map( item => item*10 )// => ํ์ค์ผ ๊ฒฝ์ฐ ๋ฆฌํด
console.log('map์ผ๋ก ์๋กญ๊ฒ ๋ง๋ค์ด์ง newArr', newArr)
return (
....
)
}
๐ filter - ์์ ๊ฐ์๋งํผ ๋ฐ๋ณตํ๋ฉฐ boolean์ผ๋ก ๋ฆฌํด๋ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฉํด์ ์๋ก์ด list๋ฅผ ๋ง๋ฌ
(๋ฐฐ์ด์ ํน์ ์กฐ๊ฑด์ ๋ง๋ ์์๋ค๋ง ์ถ์ถํ์ฌ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค ๋ ์ฌ์ฉ)
array.filter(callbackFunction(currenValue, index, array), thisArg)
- currenValue: ํ์ฌ๊ฐ
- index: ํ์ฌ์ธ๋ฑ์ค
- arrayt: ํ์ฌ๋ฐฐ์ด
- thisArg: callbackFunction ๋ด์์ this๋ก ์ฌ์ฉ๋ ๊ฐ
๐บ map ์ ์ฝ๋ฐฑํจ์์ ๋ฆฌํด๊ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์๋๋ฐ, ๋ค์ํ ์ฅ์ ์ด ์๋ค. !!
๋ฆฌํด ๊ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๋๋กํ์!
๐ map ์ฝ๋ฐฑํจ์์ ๋ฆฌํด
- map์ ์ฝ๋ฐฑํจ์์ ๋ฆฌํด์ ๋ฐ๋ณต์ํฌ ํ๊ทธ๋ฅผ ๋ฃ์ต๋๋ค.
- ๋ฆฌ์กํธ์์ key๋ ๋ฐฐ์ด์ ๋ ๋๋ง ์ํฌ๋ ๋น ๋ฅด๊ฒ ๋ณํ๋ฅผ ๊ฐ์งํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ์์ฑ์ ๋๋ค.
- key๋ index๋์ ๊ณ ์ ํ ๊ฐ์ ๋ฃ์ด ์ฃผ๋๋ก ๊ถ์ ๋ฉ๋๋ค. (key๋ฅผ ๋ฃ์ง ์์ผ๋ฉด props์๋ฌ๊ฐ ๋ฐ์ ๋ฉ๋๋ค.)
const IterationComponent = () => {
const arr = [1,2,3,4,5];
const newArr = arr.map((item, index) => {
return <li key={๊ณ ์ ํ์๋ณ๊ฐ}>{item}</li>
})
return (
<ul>
{newArr}
</ul>
)
}
export default IterationComponent;
๐บ ๋ฐฐ์ด์ ํฉ์น๋ ๋ฐฉ๋ฒ๋ ์์์ผ๊ฒ ์ง !
๐ concat ๋ฉ์๋
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3); //["a", "b", "c", "d", "e", "f"]
๐ ์ ๊ฐ๊ตฌ๋ฌธ - ์ ๊ฐ ์ฐ์ฐ์ (...)
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = [...array1, ...array2];
console.log(array3); //["a", "b", "c", "d", "e", "f"]
๐ push ๋ฉ์๋์ ์ ๊ฐ ์ฐ์ฐ์
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
arr1.push(...arr2);
console.log(arr1); // [1, 2, 3, 4, 5, 6]
์ด์ธ์๋ ํฉ์น๋ ๋ฐฉ๋ฒ์ ๋ค์ํ๋ค!
[App.js]
import { Fragment } from "react";
import IterationComponent from "./component/IterationComponent";
import IterationComponent3 from "./component/IterationComponent3";
import IterationComponent4 from "./component/IterationComponent4";
import IterationComponentQ2 from "./component/IterationComponentQ2";
function App() {
return (
<Fragment>
<IterationComponent />
<IterationComponent3 />
<IterationComponent4 />
<IterationComponentQ2 />
</Fragment>
)
}
export default App;
[IterationComponent.js]
function IterationComponent() {
const arr = [1,2,3,4,5];
// const newArr = arr.map((value, index) => {
// return value * 10;
// })
//const newArr = arr.map((value, index) => value * 10);
// li ํ๊ทธ ๋ฆฌ์กํธ elemet ๋ฐ๋์ ๊ณ ์ ํ key props ์ ๋ฌํ๋๋ก ํด์ผํจ
const newArr = arr.map((value,index) => {
return <li style={{listStyle:'none'}} key={index}> ๊ฐ : {value} </li>
} )
console.log(newArr);
return (
<div>
<ul>
{newArr}
</ul>
</div>
)
}
export default IterationComponent;
const arr = [1,2,3,4,5];
const newArr = arr.map((value, index) => {
return value * 10;
})
const newArr = arr.map((value, index) => value * 10);
const newArr = arr.map((value,index) => {
return <li style={{listStyle:'none'}} key={index}> ๊ฐ : {value} </li>
} )
- ๋ฆฌ์กํธ element ๋ฐ๋์ ๊ณ ์ ํ key props ์ ๋ฌํ๋๋ก ํด์ผํ๋ค.
[IterationComponent2.js]
import { useState } from "react";
function IterationComponent3() {
const data = [
{id:1, topic:"hi"},
{id:2, topic:"zz"},
];
let [list, setList] = useState(data);
//input ํ๊ทธ state ๋ก ๊ด๋ฆฌ
let [topic, setTopic] = useState('');
// ๋ฒํผ ํด๋ฆญ์์ input state ๊ฐ์ ๋ฐฐ์ด์๋ค๊ฐ ์ถ๊ฐํ๊ธฐ
let handleClick = () => {
let obj = {id : list[list.length-1].id + 1, topic : topic };
let newList = list.concat(obj);
//console.log(newList);
setList(newList); // ๋ค์ ๊ทธ๋ฆฌ๊ฒ๋จ state ์
๋ฐ์ดํธ ๋ฆฌ๋ ๋๋ง
setTopic('');
}
//๋๋ธํด๋ฆญ ์ด๋ฒคํธ
let handleRemove = (id) => {
// filter ํด๋น๊ฐ์ด
let newList = list.filter((value) => {
return value.id !== id; // ๋ฆฌํด์ ์ค๋ฆฐ ๊ฐ์ด true ์ธ ๋ฐ์ดํฐ๋ง ํํฐ๋ง
} )
setList(newList);
}
// ๋ฆฌ์คํธ๋ก ๋ฐ๋ณต ์ฒ๋ฆฌ - ์ด๋ฒคํธ๊ฐ ํ๊ทธ๋ณด๋ค ์์ชฝ์ ์์นํด์ผํจ
// ์ด๋ฒคํธํจ์์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฃผ๊ณ ์ถ์ผ๋ฉด () => {์คํ์ํฌํจ์(๋งค๊ฐ๊ฐ)}
const newList = list.map( value => <li style={{color:'blue'}}
key={value.id}
onDoubleClick={()=>{handleRemove(value.id)}}> {value.topic} </li>)
return (
<div>
<h3> ๊ฐ๋จํ ํ ์ผ ๋ชฉ๋ก ๋ง๋ค๊ธฐ </h3>
<ul>
{newList}
</ul>
<input type="text" value={topic} onChange={(e)=> setTopic(e.target.value)}/>
<button type="button" onClick={handleClick}> ์ถ๊ฐํ๊ธฐ </button>
</div>
)
}
export default IterationComponent3;
const data = [
{id:1, topic:"hi"},
{id:2, topic:"zz"},
];
let [list, setList] = useState(data);
//input ํ๊ทธ state ๋ก ๊ด๋ฆฌ
let [topic, setTopic] = useState('');
// ๋ฒํผ ํด๋ฆญ์์ input state ๊ฐ์ ๋ฐฐ์ด์๋ค๊ฐ ์ถ๊ฐํ๊ธฐ
let handleClick = () => {
let obj = {id : list[list.length-1].id + 1, topic : topic };
let newList = list.concat(obj);
//console.log(newList);
setList(newList); // ๋ค์ ๊ทธ๋ฆฌ๊ฒ๋จ state ์
๋ฐ์ดํธ ๋ฆฌ๋ ๋๋ง
setTopic('');
}
- ํด๋ฆญ์ ๋ฐฐ์ด์ ๊ฐ ์ถ๊ฐ
//๋๋ธํด๋ฆญ ์ด๋ฒคํธ
let handleRemove = (id) => {
// filter ํด๋น๊ฐ์ด
let newList = list.filter((value) => {
return value.id !== id; // ๋ฆฌํด์ ์ค๋ฆฐ ๊ฐ์ด true ์ธ ๋ฐ์ดํฐ๋ง ํํฐ๋ง
} )
setList(newList);
}
- ๋๋ธํด๋ฆญ ์ด๋ฒคํธ
// ๋ฆฌ์คํธ๋ก ๋ฐ๋ณต ์ฒ๋ฆฌ - ์ด๋ฒคํธ๊ฐ ํ๊ทธ๋ณด๋ค ์์ชฝ์ ์์นํด์ผํจ
// ์ด๋ฒคํธํจ์์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฃผ๊ณ ์ถ์ผ๋ฉด () => {์คํ์ํฌํจ์(๋งค๊ฐ๊ฐ)}
const newList = list.map( value => <li style={{color:'blue'}}
key={value.id}
onDoubleClick={()=>{handleRemove(value.id)}}> {value.topic} </li>)
<ul>
{newList}
</ul>
<input type="text" value={topic} onChange={(e)=> setTopic(e.target.value)}/>
<button type="button" onClick={handleClick}> ์ถ๊ฐํ๊ธฐ </button>
[IterationComponent3.js]
import { useState } from "react";
//import img1 from '../img/img1.png';
/*
์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ
1. ์ธ๋ถ ์๋ฒ์์ ๊ฒฝ๋ก ์ฐธ์กฐ ๋ฐ๊ธฐ (๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ)
2. src๋ฐ์ผ๋ก ๋ฃ๋๊ฒฝ์ฐ (img1 ์ผ๋ก ํ๋์ ์ด๋ฏธ์ง ์ฐธ์กฐ๊ฐ๋ฅ)
import img1 from '../img/1.jpg';
3. public๋ฐ์ผ๋ก ๋ฃ๋๊ฒฝ์ฐ๋ ๋ฐ๋ก ์ฐธ์กฐํ ์ ์์ต๋๋ค.
*/
const IterationComponent4 = () => {
//๋ฐ์ดํฐ - publicํด๋๋ฐ์ ์์๋ก ์ฌ์ฉํ๊ธฐ ์ํด ๋ฃ์ต๋๋ค.
const arr = [
{src : '/img/img1.png', title : '์์ดํฐ10', price: 1000},
{src : '/img/img2.png', title : '์์ดํฐ11', price: 2000},
{src : '/img/img3.png', title : '์์ดํฐ12', price: 3000},
{src : '/img/img4.png', title : '์์ดํฐ13', price: 4000},
]
let [img, setImg] = useState('');
//๋ถ๋ชจ์๊ฒ ๊ฑธ๋ฉด ์์๋ ๊ฑธ๋ฆฌ๋ ํน์ฑ์ ์ด์ฉํด๋ ๋๋ค.
//const [im,setIm] = useState('"/img/img1.png"')
let handleClick = (e) => {
console.log(e.target);
console.log(e.target.src);
// let obj = {id : list[list.length-1].id + 1, topic : topic };
// let newList = list.concat(obj);
// //console.log(newList);
// setList(newList); // ๋ค์ ๊ทธ๋ฆฌ๊ฒ๋จ state ์
๋ฐ์ดํธ ๋ฆฌ๋ ๋๋ง
// setTopic('');
setImg(e.target.src);
// if (e.target.tagName != "IMG") return;
// let img = e.target.src ;
// setImg(img);
}
// ๋ชฉ๋ก ์ถ๋ ฅ IMG ๋ ๊ฐ ํ๋์ ๊ฐ์ฒด๊ฐ ๋๋ค !!
const newArr = arr.map((img, index) => {
return <div key={index} > <img src={img.src} onClick={handleClick} /> <p style={{color:'blue'}}> ์ํ : {img.title} </p> <p> ๊ฐ๊ฒฉ :{img.price}</p> </div>
} )
return (
<div>
<hr/>
<h3>์ด๋ฏธ์ง ๋ฐ์ดํฐ ๋ฐ๋ณตํด๋ณด๊ธฐ</h3>
{/* <img src="https://raw.githubusercontent.com/yopy0817/data_example/master/img/img1.png" alt="๋ด" width="100" /> */}
{/* <img src={img1}> */}
{/* <img src="/img/img1.png" /> */}
<img src={img} width={'300px'}/>
{/*
<div>
<div>
<img src="/img/img1.png" width="100" />
<p>
์ ๋ชฉ
</p>
<p>
๊ฐ๊ฒฉ
</p>
</div>
<div>
*/}
{/* 3๋ฒ */}
<div style={{display:'flex', textAlign:'center', fontWeight:'600'}}>
{newArr}
</div>
</div>
)
}
export default IterationComponent4;
//๋ฐ์ดํฐ - publicํด๋๋ฐ์ ์์๋ก ์ฌ์ฉํ๊ธฐ ์ํด ๋ฃ์ต๋๋ค.
const arr = [
{src : '/img/img1.png', title : '์์ดํฐ10', price: 1000},
{src : '/img/img2.png', title : '์์ดํฐ11', price: 2000},
{src : '/img/img3.png', title : '์์ดํฐ12', price: 3000},
{src : '/img/img4.png', title : '์์ดํฐ13', price: 4000},
]
let [img, setImg] = useState('');
//๋ถ๋ชจ์๊ฒ ๊ฑธ๋ฉด ์์๋ ๊ฑธ๋ฆฌ๋ ํน์ฑ์ ์ด์ฉํด๋ ๋๋ค.
//const [im,setIm] = useState('"/img/img1.png"')
let handleClick = (e) => {
console.log(e.target);
console.log(e.target.src);
// let obj = {id : list[list.length-1].id + 1, topic : topic };
// let newList = list.concat(obj);
// //console.log(newList);
// setList(newList); // ๋ค์ ๊ทธ๋ฆฌ๊ฒ๋จ state ์
๋ฐ์ดํธ ๋ฆฌ๋ ๋๋ง
// setTopic('');
setImg(e.target.src);
// if (e.target.tagName != "IMG") return;
// let img = e.target.src ;
// setImg(img);
}
- ํด๋ฆญ ์ด๋ฒคํธ : ์ด๋ฏธ์ง ํด๋ฆญ์ ๋ฉ์ธ ์ด๋ฏธ์ง ๋ณ๊ฒฝ๋จ
// ๋ชฉ๋ก ์ถ๋ ฅ IMG ๋ ๊ฐ ํ๋์ ๊ฐ์ฒด๊ฐ ๋๋ค !!
const newArr = arr.map((img, index) => {
return <div key={index} > <img src={img.src} onClick={handleClick} /> <p style={{color:'blue'}}> ์ํ : {img.title} </p> <p> ๊ฐ๊ฒฉ :{img.price}</p> </div>
} )
<img src="https://raw.githubusercontent.com/yopy0817/data_example/master/img/img1.png" alt="๋ด" width="100" />
<img src={img1}>
<img src="/img/img1.png" />
- ์ด๋ฏธ์ง ๋์ฐ๊ธฐ
<div>
<hr/>
<h3>์ด๋ฏธ์ง ๋ฐ์ดํฐ ๋ฐ๋ณตํด๋ณด๊ธฐ</h3>
<img src={img} width={'300px'}/>
<div style={{display:'flex', textAlign:'center', fontWeight:'600'}}>
{newArr}
</div>
</div>
[IterationComponentQ2.js]
import { useState } from "react";
const IterationComponentQ2 = () => {
//1 - select๋ ์ปดํฌ๋ํธ๋ฐ๋ณต์ผ๋ก optionํ๊ทธ๋ฅผ ์์ฑํฉ๋๋ค.
const select = ['HTML', 'Javascript', 'CSS', 'Java', 'Oracle', 'Mysql'];
//2 - ์๋ ๋ฐ์ดํฐ๋ state๋ก ๊ด๋ฆฌํ๊ณ ํ๋ฉด์ liํ๊ทธ๋ก ๋ฐ๋ณตํด์ฃผ์ธ์ .
const data = [
{id: 1, type: 'Java', teacher: '์ด์์ '},
{id: 2, type: 'Java', teacher: 'ํ๊ธธ์'},
{id: 3, type: 'Javascript', teacher: 'ํ๊ธธ๋'},
{id: 4, type: 'Oracle', teacher: '์ด์์ '},
{id: 5, type: 'Mysql', teacher: '์ด์์ '},
{id: 6, type: 'CSS', teacher: '๋ฐ์ฐฌํธ'},
{id: 7, type: 'HTML', teacher: 'coding404'},
];
const [list, setList] = useState(data);
//3 - ์
๋ ํธ๋ฐ์ค๊ฐ ์ฒด์ธ์ง๋๋ฉด ์ด๋ฒคํธ๊ฐ์ฒด๋ฅผ ํ์ฉํด์ ์ ํ๋ ๊ฐ๋ง ํํฐ๋งํด์ฃผ์ธ์.
let handleChange = (e) => {
//data์์ ํํฐ๋ง
let newList = data.filter( item => item.type === e.target.value );
setList(newList);
}
//4- input๊ฐ์ state๋ก ๊ด๋ฆฌํฉ๋๋ค.
//5 - ๊ฒ์๊ธฐ๋ฅ - ์๋ณธ data์์ ํํฐ๋ง์ผ๋ก ๊ฒ์๊ฐ์ ์ฐพ์ state๋ฅผ ์
๋ฐ์ดํธ ํฉ๋๋ค.
const [input, setInput] = useState('');
const handleClick = () => {
let newList = data.filter( item => {
let str = item.type.concat(item.teacher).toLowerCase(); //๋ฌธ์์ด ๋ถ์ด๊ธฐ
return str.includes(input); //ํฌํจ๋์ด ์์ผ๋ฉด true
});
setList(newList); //state๋ณ๊ฒฝ
}
return (
<div>java
<hr/>
<h3>์ค์ต</h3>
Search: <input type="text" value={input} onChange={ (e) => setInput(e.target.value) }/>
<button onClick={handleClick} >๊ฒ์</button>
<br/>
๊ณผ๋ชฉ์ฐพ๊ธฐ:
<select onChange={handleChange}>
{
select.map( (item, index) => <option key={index}>{item}</option>)
}
</select>
<ul>
{
list.map( item => <li key={item.id}>{item.type}-{item.teacher}</li>)
}
</ul>
</div>
)
}
export default IterationComponentQ2;