기본문법
arr.map(callback, [thisArg])
주의사항
map() 을 사용할때 꼭 key 값 설정을 해줘야 함
관련 예제
IterationSample Component 를 만들어서 import 시키고 랜더링 시키기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import React, { Component } from "react";
import IterationSample from "./IterationSample";
class App extends Component {
render() {
return (
<div>
<IterationSample />
</div>
);
}
}
export default App;
|
cs |
IterationSample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import React, { Component } from "react";
class IterationSample extends Component {
state = {
names: ["눈사람", "얼음", "눈", "바람"],
name: "",
};
handleChange = (e) => {
this.setState({
name: e.target.value,
});
};
handleInsert = () => {
this.setState({
names: this.state.names.concat(this.state.name),
name: "",
});
};
handleRemove = (index) => {
const { names } = this.state;
this.setState({
names: [
...names.slice(0, index),
...names.slice(index + 1, names.length),
],
});
};
render() {
const nameList = this.state.names.map((name, index) => (
<li key={index} onDoubleClick={() => this.handleRemove(index)}>
{name}
{index}
</li>
));
return (
<div>
<input onChange={this.handleChange} value={this.state.name} />
<button onClick={this.handleInsert}>추가</button>
<ul>{nameList}</ul>
</div>
);
}
}
export default IterationSample;
|
cs |
26~27번째 라인의 ...은 재개 연산자로
...뒤에 위치한 배열 값을 그대로 꺼내서 현재 배열에 복사하는 것
배열을 잘라내는 함수 slice() 에 대해 잘 정리해놓은 내용
[JS/Array] slice()와 splice()의 차이점
slice()와 splice()는 배열을 다룰 때 자주 사용하는 함수이다. 두 함수는 언뜻 보기에 비슷한 기능을 하는 것처럼 보이지만 큰 차이점이 있다. [1] Array.prototype.slice() slice() 메소드는 begin부터 end..
im-developer.tistory.com
[Javascript] Array의 slice와 splice의 차이
slice() slice 함수는 Array에서 String의 substring와 같은 기능을 가지고 있는 함수이다. 즉 Array의 내용의 시작과 끝을 받아서 뽑아주는 기능이다. 문법을 보자면 아래와 같다. arr.slice([begin[, end]]) 발..
secondmemory.kr
slice 한 배열을 concat 으로 붙이는것보다
재개 연산자로 표현하는것이 코듸도 짧고 가독성이 좋다.
'React' 카테고리의 다른 글
Component 의 LifeCycle 예제 (0) | 2021.04.22 |
---|---|
Component 의 LifeCycle (0) | 2021.04.22 |
Component 에 ref 설정하기 (0) | 2021.04.20 |
ValidationSample.js ( ref 함수 사용 + 유효성 검사 예제 ) (0) | 2021.04.20 |
EventPractice2 ( input 여러 개를 핸들링 ) (0) | 2021.04.20 |