[JavaScript] Arrays.filter() [ 특정 값 or 배열만 남기고 싶을 때]
리트리버J
·2021. 1. 17. 11:35
728x90
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | class App extends Component { constructor(props) { super(props); this.max_content_id = 3; this.state = { mode:"create", subject: {title: 'WEB', sub: "World Wide Web!"}, welcome: { title: "Welcome", sub: "Hello, React!" }, selected_content_id : 2, contents: [ {id:1, title:'HTML', desc:'HTML is HyperText ...'}, {id:2, title:'CSS', desc:'CSS is Design ...'}, {id:3, title:'JavaScript', desc:'HTML is JavaScript ...'}, ] } } getReadContent() { return this.state.contents.filter(function (element) { if (element.id === this.state.selected_content_id) { return element; } }.bind(this)) } getContent() { var _title, _desc, _article = null; if (this.state.mode === "welcome") { _title = this.state.welcome.title; _desc = this.state.welcome.sub; _article = <ReadContent title={_title} desc={_desc}></ReadContent>; } else if (this.state.mode === "read") { console.log(this.getReadContent()); var _content = this.getReadContent(); console.log(_content); _article = <ReadContent title={_content.title} desc={_content.desc}></ReadContent> } else if (this.state.mode === "create") { _article = <CreateContent onSubmit={function (_title, _desc) { this.max_content_id = this.max_content_id + 1; // this.state.contents.push( // { id: this.max_content_id, title: _title, desc: _desc } // ); var _contents = this.state.contents.concat( { id: this.max_content_id, title: _title, desc: _desc } ) this.setState({ // contents: this.state.contents contents: _contents }) }.bind(this)}></CreateContent> } else if (this.state.mode === "update") { _article = <UpdateContent onSubmit={function (_title, _desc) { this.max_content_id = this.max_content_id + 1; // this.state.contents.push( // { id: this.max_content_id, title: _title, desc: _desc } // ); var _contents = this.state.contents.concat( { id: this.max_content_id, title: _title, desc: _desc } ) this.setState({ // contents: this.state.contents contents: _contents }) }.bind(this)}></UpdateContent> } return _article; } render() { return ( <div className="App"> <Subject title={this.state.subject.title} sub={this.state.subject.sub} onChangePage={function () { this.setState({ mode: "welcome" }); }.bind(this)} > </Subject> <TOC onChangePage={function (id) { this.setState({ mode: "read", selected_content_id:Number(id) }); }.bind(this)} data={this.state.contents}> </TOC> <Control onChangeMode={function (_mode) { this.setState({ mode: _mode }) }.bind(this)}></Control> {this.getContent()} </div> ); } } | cs |
728x90
'Programming > 자바스크립트' 카테고리의 다른 글
[JavaScript/JQuery]Ajax 통신 시, HTTP 0 error 코드 원인 (0) | 2021.02.18 |
---|---|
[JavaScript]배열 특정 요소 제거 (0) | 2021.02.02 |
[JavaScript/JSON] JAVA에서 double 0.0으로 보냈는데 jsp에서 0일때 (0) | 2020.11.15 |
[JavaScript] ajax와 ROWNUM을 통한 무한스크롤 (0) | 2020.11.15 |
[JavaScript] 카테고리 필터로 원하는 값 표시하기 (0) | 2020.11.11 |