{"id":32881,"date":"2024-11-01T09:12:12","date_gmt":"2024-11-01T09:12:12","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32881"},"modified":"2024-11-01T11:21:37","modified_gmt":"2024-11-01T11:21:37","slug":"react-course-arrays-and-methods","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32881\/","title":{"rendered":"React Course: Arrays and Methods"},"content":{"rendered":"<p><body><\/p>\n<p>React is one of the JavaScript libraries optimized for building user interfaces (UI). In many cases, working with arrays is essential when constructing UIs, and effectively using arrays and their associated methods in JavaScript is crucial. In this tutorial, we will explore various methods for handling arrays in React and how to utilize them.<\/p>\n<h2>1. Understanding Arrays in JavaScript<\/h2>\n<p>An array is a collection of ordered data. In JavaScript, arrays can be used to store and process multiple pieces of data. Arrays provide various methods that allow us to manipulate their contents.<\/p>\n<h3>1.1 Creating Arrays<\/h3>\n<p>There are several ways to create an array. The basic methods to create arrays are as follows.<\/p>\n<pre><code>const array1 = [];\nconst array2 = [1, 2, 3];\nconst array3 = new Array(4, 5, 6);<\/code><\/pre>\n<h3>1.2 Array Properties<\/h3>\n<p>Arrays have several useful properties. Among them, the <code>length<\/code> property allows you to check the length of the array.<\/p>\n<pre><code>console.log(array2.length); \/\/ 3<\/code><\/pre>\n<h2>2. Array Methods<\/h2>\n<p>Array methods provide various functionalities that are useful for working with arrays. Below, we will explain commonly used array methods.<\/p>\n<h3>2.1 push()<\/h3>\n<p>The <code>push()<\/code> method adds one or more elements to the end of an array.<\/p>\n<pre><code>const arr = [1, 2];\narr.push(3);\nconsole.log(arr); \/\/ [1, 2, 3]<\/code><\/pre>\n<h3>2.2 pop()<\/h3>\n<p>The <code>pop()<\/code> method removes the last element from an array and returns that element.<\/p>\n<pre><code>const arr = [1, 2, 3];\nconst lastElement = arr.pop();\nconsole.log(lastElement); \/\/ 3\nconsole.log(arr); \/\/ [1, 2]<\/code><\/pre>\n<h3>2.3 shift()<\/h3>\n<p>The <code>shift()<\/code> method removes the first element from an array and returns that element.<\/p>\n<pre><code>const arr = [1, 2, 3];\nconst firstElement = arr.shift();\nconsole.log(firstElement); \/\/ 1\nconsole.log(arr); \/\/ [2, 3]<\/code><\/pre>\n<h3>2.4 unshift()<\/h3>\n<p>The <code>unshift()<\/code> method adds one or more elements to the beginning of an array.<\/p>\n<pre><code>const arr = [2, 3];\narr.unshift(1);\nconsole.log(arr); \/\/ [1, 2, 3]<\/code><\/pre>\n<h3>2.5 splice()<\/h3>\n<p>The <code>splice()<\/code> method is used to add or remove elements from an array.<\/p>\n<pre><code>const arr = [1, 2, 3, 4];\narr.splice(2, 1, 'a', 'b'); \nconsole.log(arr); \/\/ [1, 2, 'a', 'b', 4]<\/code><\/pre>\n<h3>2.6 slice()<\/h3>\n<p>The <code>slice()<\/code> method copies a portion of an array and returns a new array.<\/p>\n<pre><code>const arr = [1, 2, 3, 4];\nconst newArr = arr.slice(1, 3);\nconsole.log(newArr); \/\/ [2, 3]<\/code><\/pre>\n<h3>2.7 forEach()<\/h3>\n<p>The <code>forEach()<\/code> method executes a given function on each element of the array.<\/p>\n<pre><code>const arr = [1, 2, 3];\narr.forEach(element =&gt; {\n    console.log(element); \/\/ 1, 2, 3\n});<\/code><\/pre>\n<h3>2.8 map()<\/h3>\n<p>The <code>map()<\/code> method returns a new array containing the results of calling a given function on every element in the array.<\/p>\n<pre><code>const arr = [1, 2, 3];\nconst doubled = arr.map(num =&gt; num * 2);\nconsole.log(doubled); \/\/ [2, 4, 6]<\/code><\/pre>\n<h3>2.9 filter()<\/h3>\n<p>The <code>filter()<\/code> method creates a new array with all elements that pass the test implemented by the provided function.<\/p>\n<pre><code>const arr = [1, 2, 3, 4];\nconst evenNumbers = arr.filter(num =&gt; num % 2 === 0);\nconsole.log(evenNumbers); \/\/ [2, 4]<\/code><\/pre>\n<h3>2.10 reduce()<\/h3>\n<p>The <code>reduce()<\/code> method executes a function on each element of the array, resulting in a single value.<\/p>\n<pre><code>const arr = [1, 2, 3, 4];\nconst sum = arr.reduce((accumulator, currentValue) =&gt; accumulator + currentValue, 0);\nconsole.log(sum); \/\/ 10<\/code><\/pre>\n<h2>3. Utilizing Arrays in React<\/h2>\n<p>When developing React applications, arrays play a crucial role. They can be efficiently utilized to dynamically update user interfaces or process user inputs.<\/p>\n<h3>3.1 Managing Arrays as State<\/h3>\n<p>React components often manage arrays as state. Here\u2019s an example of using an array as state.<\/p>\n<pre><code>import React, { useState } from 'react';\n\nconst App = () =&gt; {\n    const [items, setItems] = useState(['Apple', 'Banana', 'Cherry']);\n\n    const addItem = () =&gt; {\n        setItems([...items, 'Strawberry']);\n    };\n\n    return (\n        <div>\n            <h1>Fruit List<\/h1>\n            <ul>\n                {items.map((item, index) =&gt; (\n                    <li key=\"{index}\">{item}<\/li>\n                ))}\n            <\/ul>\n            <button onclick=\"{addItem}\">Add Strawberry<\/button>\n        <\/div>\n    );\n};\n\nexport default App;<\/code><\/pre>\n<h3>3.2 Conditional Rendering<\/h3>\n<p>Conditional rendering can be implemented using array methods. For example, rendering only elements that meet a specific condition can be done in various ways.<\/p>\n<pre><code>const App = () =&gt; {\n    const items = ['Apple', 'Banana', 'Cherry', 'Strawberry'];\n\n    return (\n        <div>\n            <h1>Fruit List<\/h1>\n            <ul>\n                {items.filter(item =&gt; item.startsWith('A')).map((item, index) =&gt; (\n                    <li key=\"{index}\">{item}<\/li>\n                ))}\n            <\/ul>\n        <\/div>\n    );\n};<\/code><\/pre>\n<h3>3.3 Updating Elements of an Array<\/h3>\n<p>There are situations where only specific elements of an array need to be modified when updating state. In this case, <code>map()<\/code> can be used with <code>setState<\/code>.<\/p>\n<pre><code>const App = () =&gt; {\n    const [items, setItems] = useState(['Apple', 'Banana', 'Cherry']);\n\n    const updateItem = (index) =&gt; {\n        const newItems = items.map((item, i) =&gt; (i === index ? 'Updated Fruit' : item));\n        setItems(newItems);\n    };\n\n    return (\n        <div>\n            <h1>Fruit List<\/h1>\n            <ul>\n                {items.map((item, index) =&gt; (\n                    <li key=\"{index}\">\n                        {item}\n                        <button onclick=\"{() => updateItem(index)}>Update<\/button>\n                    <\/li>\n                ))}\n            <\/ul>\n        <\/div>\n    );\n};<\/code><\/pre>\n<h2>4. Conclusion<\/h2>\n<p>In this tutorial, we explored how to handle arrays and their methods in React. Understanding and utilizing JavaScript&#8217;s array methods is a very useful skill for developing React applications. By learning how to manage arrays efficiently and update data states, you can write better performance and more readable code in React.<\/p>\n<p>As you develop real applications using React, you&#8217;ll find yourself utilizing more array methods, and it&#8217;s important to learn the characteristics and uses of each method. I hope this tutorial helps you in your React development endeavors.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is one of the JavaScript libraries optimized for building user interfaces (UI). In many cases, working with arrays is essential when constructing UIs, and effectively using arrays and their associated methods in JavaScript is crucial. In this tutorial, we will explore various methods for handling arrays in React and how to utilize them. 1. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32881\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: Arrays and Methods&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[123],"tags":[],"class_list":["post-32881","post","type-post","status-publish","format-standard","hentry","category-react-basics-course"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>React Course: Arrays and Methods - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/32881\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: Arrays and Methods - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"React is one of the JavaScript libraries optimized for building user interfaces (UI). In many cases, working with arrays is essential when constructing UIs, and effectively using arrays and their associated methods in JavaScript is crucial. In this tutorial, we will explore various methods for handling arrays in React and how to utilize them. 1. &hellip; \ub354 \ubcf4\uae30 &quot;React Course: Arrays and Methods&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32881\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:37+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32881\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32881\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: Arrays and Methods\",\"datePublished\":\"2024-11-01T09:12:12+00:00\",\"dateModified\":\"2024-11-01T11:21:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32881\/\"},\"wordCount\":505,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32881\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32881\/\",\"name\":\"React Course: Arrays and Methods - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:12+00:00\",\"dateModified\":\"2024-11-01T11:21:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32881\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32881\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32881\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: Arrays and Methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"React Course: Arrays and Methods - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/32881\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: Arrays and Methods - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"React is one of the JavaScript libraries optimized for building user interfaces (UI). In many cases, working with arrays is essential when constructing UIs, and effectively using arrays and their associated methods in JavaScript is crucial. In this tutorial, we will explore various methods for handling arrays in React and how to utilize them. 1. &hellip; \ub354 \ubcf4\uae30 \"React Course: Arrays and Methods\"","og_url":"https:\/\/atmokpo.com\/w\/32881\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:12+00:00","article_modified_time":"2024-11-01T11:21:37+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32881\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32881\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: Arrays and Methods","datePublished":"2024-11-01T09:12:12+00:00","dateModified":"2024-11-01T11:21:37+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32881\/"},"wordCount":505,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32881\/","url":"https:\/\/atmokpo.com\/w\/32881\/","name":"React Course: Arrays and Methods - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:12+00:00","dateModified":"2024-11-01T11:21:37+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32881\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32881\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32881\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: Arrays and Methods"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=32881"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32881\/revisions"}],"predecessor-version":[{"id":32882,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32881\/revisions\/32882"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}