<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tanuka Das</title>
    <description>The latest articles on DEV Community by Tanuka Das (@tanuka16).</description>
    <link>https://dev.to/tanuka16</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F463739%2F2486fcff-c412-492c-9667-a94788a19d22.jpeg</url>
      <title>DEV Community: Tanuka Das</title>
      <link>https://dev.to/tanuka16</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tanuka16"/>
    <language>en</language>
    <item>
      <title>12 Must Know Array Methods for the Next Interview - JavaScript</title>
      <dc:creator>Tanuka Das</dc:creator>
      <pubDate>Sun, 04 Oct 2020 20:32:24 +0000</pubDate>
      <link>https://dev.to/tanuka16/12-must-know-array-methods-for-the-next-interview-javascript-5dmo</link>
      <guid>https://dev.to/tanuka16/12-must-know-array-methods-for-the-next-interview-javascript-5dmo</guid>
      <description>&lt;p&gt;One thing common about every programmer, be it senior, junior, or entry-level we often look up syntaxes and methods while coding but it is not possible during an interview. Although many interviewers help and give hints if you get stuck, it is a good practice to have some of the methods memorized.&lt;/p&gt;

&lt;h2&gt;
  
  
  Array Methods
&lt;/h2&gt;

&lt;p&gt;1.&lt;code&gt;push()&lt;/code&gt; method is used to add items at the end of an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const books = ['Cracking the Coding Interview', 'C++ Without Fear'];
books.push('JavaScript');
books;

#Output: ['Cracking the Coding Interview', 'C++ Without Fear', 'JavaScript']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;2.&lt;code&gt;unshift()&lt;/code&gt; add values at beginning of the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;books.unshift('');
books;

#Output: ['Cracking the Coding Interview', 'C++ Without Fear', 'JavaScript']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3.&lt;code&gt;.pop()&lt;/code&gt;removes the final element from the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cartoons = ['Bugs Bunny', 'Scooby-Doo', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora', 'Pooh']
cartoons.pop();

# 'Pooh'
certoons;

# Output: ['Bugs Bunny', 'Scooby-Doo', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;4.&lt;code&gt;.shift()&lt;/code&gt; removes the very first element from the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cartoons = ['Scooby-Doo', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora', 'Pooh']
cartoons.shift();
# 'Bugs Bunny'
certoons;

# Output: ['Bugs Bunny', 'Scooby-Doo', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora', 'Pooh']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;5.The &lt;code&gt;.slice()&lt;/code&gt; method, as the name implies, slices out a portion of the array. It doesn’t remove any element from the array, instead, it returns a copy of the original array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cartoons = ['Scooby-Doo', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora', 'Pooh']
cartoons.slice();
certoons;

# Output: ['Scooby-Doo', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora', 'Pooh']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.slice()&lt;/code&gt; method takes in two parameters, the index where the slice starts and the index before where the slice ends.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cartoons = ['Scooby-Doo', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora', 'Pooh']

cartoons.slice(1, 3);
# Output:['Mickey Mouse', 'The Powerpuff Girls']
If we pass in only one parameter, the .slice() method will slice from the given index to the end of the array. 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cartoons = ['Scooby-Doo', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora', 'Pooh']

cartoons.slice(2);
# Output: ['The Powerpuff Girls', 'Dora', 'Pooh']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;6.&lt;code&gt;.splice()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.splice()&lt;/code&gt; method is used to add, replace, and remove items to an array.&lt;/p&gt;

&lt;p&gt;It can hold multiple parameters, the first refers to the index where the element will be placed. The second argument refers to the number of elements that will be removed. Every parameter after the first two defines the elements that should be added in the array.&lt;/p&gt;

&lt;p&gt;Let’s take a look at the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cartoons = ['Scooby-Doo', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora', 'Pooh']
// # 1
cartoons.splice(1, 0, 'SpongeBob');
// add 'SpongeBob' at index 1
// remove 0 elements
//Output: ['Scooby-Doo', 'SpongeBob', 'Mickey Mouse', 'The Powerpuff Girls', 'Dora', 'Pooh']
// # 2
cartoons.splice(4, 2, 'Patrick Star');
// add 'Patrick Star' at index 5
// remove 2 elements, starting at index 4, which is first given parameter
// Output: ['Scooby-Doo', 'SpongeBob', 'Mickey Mouse', 'The Powerpuff Girls', 'Patrick Star']
// # 3
cartoons.splice(2, 1);
// The item at index 2 will be removed as there aren't any defined parameter to replace it with
// remove 1 elements
// Output: ['Scooby-Doo', 'SpongeBob', 'The Powerpuff Girls', 'Patrick Star']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;7.&lt;code&gt;.filter()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.filter()&lt;/code&gt; method, pass in a callback function, which is called on each element in the array. It takes in the element as a parameter and returns a boolean value based on if the element should be included in the new array or not.&lt;/p&gt;

&lt;p&gt;Let’s look at the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const movies = [
  { name: 'Wonder Woman', year: 2017 },
  { name: 'Dark Phoenix', year: 2019 },
  { name: 'Spider-Man Homecoming', year: 2017 },
  { name: 'Avengers Endgame', year: 2019 },
  { name: 'The Dark Knight', year: 2008 }
]
const filterMovies = movies.filter((movie) =&amp;gt; {
   return movie.year &amp;lt;= 2017 
})
//test filterMovie
console.log(filterMovies)
/*[
  { name: 'Wonder Woman', year: 2017 },
  { name: 'Spider-Man Homecoming', year: 2017 },
  { name: 'The Dark Knight', year: 2008 }
]*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, the new array must include every movie that was released before or in 2017. So, when the filter method is called, it loops through the movies array and executes the callback function on each element in the array. If the element matches the boolean statement, it will add the element to the new array.&lt;/p&gt;

&lt;p&gt;Note: The filter method doesn’t change the original array, only creates a new array.&lt;/p&gt;

&lt;p&gt;8.&lt;code&gt;.map()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.map()&lt;/code&gt; method maps through each element in the original array and converts it into a new array with all mapped elements. Let’s try to get every name from the movies array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const movies = [
  { name: 'Wonder Woman', year: 2017 },
  { name: 'Dark Phoenix', year: 2019 },
  { name: 'Spider-Man Homecoming', year: 2017 },
  { name: 'Avengers Endgame', year: 2019 },
  { name: 'The Dark Knight', year: 2008 }
]
const moviesName = movies.map((movie) =&amp;gt; {
   return movie.name 
})
console.log(moviesName)
// ['Wonder Woman', 'Dark Phoenix', 'Spider-Man Homecoming', 'Avengers Endgame', 'The Dark Knight']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Similar to the &lt;code&gt;.filter()&lt;/code&gt; method, &lt;code&gt;.map()&lt;/code&gt; takes in a callback function with a single parameter, and returns the new array with the elements we want, in this case, movie.name.&lt;/p&gt;

&lt;p&gt;9.&lt;code&gt;.find()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The purpose of the &lt;code&gt;.find()&lt;/code&gt; method is to find a single object in the array. It returns only the first element it can find that satisfice the provided condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const movies = [
  { name: 'Wonder Woman', year: 2017 },
  { name: 'Dark Phoenix', year: 2019 },
  { name: 'Spider-Man Homecoming', year: 2017 },
  { name: 'Avengers Endgame', year: 2019 },
  { name: 'The Dark Knight', year: 2008 }
]
const findMovie = movies.find((movie) =&amp;gt; {
   return movie.name === 'Dark Phoenix'
})
//Output: { name: 'Dark Phoenix', year: 2019 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;10.&lt;code&gt;.forEach()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.forEach()&lt;/code&gt; method is very similar to for loop but it takes in a function, and an argument, movie and for every single movie it will print out its name, &lt;code&gt;movie.name&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const movies = [
  { name: 'Wonder Woman', year: 2017 },
  { name: 'Dark Phoenix', year: 2019 },
  { name: 'Spider-Man Homecoming', year: 2017 },
  { name: 'Avengers Endgame', year: 2019 },
  { name: 'The Dark Knight', year: 2008 }
]
movies.forEach((movie) =&amp;gt; {
   console.log(movie.name)
})
// Wonder Woman
// Dark Phoenix
// Spider-Man Homecoming
// Avengers Endgame
// The Dark Knight
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We get all the names of the movies; we can even print out the years, movie.year, or any other element from inside the array. This makes iterating through an array much easier and simpler.&lt;/p&gt;

&lt;p&gt;11.&lt;code&gt;.reduce()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.reduce()&lt;/code&gt; method runs a function on every element in the array and returns the reduced single value of the array. Let’s use a test score array for this example and retrieve the total score of all the different elements in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const testScore = [
  { name: 'Ben',  score: 88 },
  { name: 'Joel', score: 98 },
  { name: 'Judy', score: 77 },
  { name: 'Anne', score: 88 },
  { name: 'Mike', score: 99 }
]
const filterMovies = testScore.reduce((currentTotal, score) =&amp;gt; {
   return test.score + currentTotal
}, 0)
//Output: 450
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first method &lt;code&gt;currentTotal&lt;/code&gt;, is the total after each iteration of the array and the second method score is the element we will be iterating through. The &lt;code&gt;currentTotal&lt;/code&gt; will start at the very first iteration, at index 0 which we passed in as the second parameter.&lt;/p&gt;

&lt;p&gt;The first time the reduce runs, we get 0, add that to Ben’s score, so 0 + 88 = 88. Now, 88 is the &lt;code&gt;currentTotal&lt;/code&gt;, and the next element Joel’s score is the score value 98, 88+98= 186. 186 is the new &lt;code&gt;currentTotal&lt;/code&gt; and it iterates until the very last score in the array. The output is 450, that’s the number we get after adding all the numbers.&lt;/p&gt;

&lt;p&gt;12.&lt;code&gt;.includes()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.include()&lt;/code&gt; method checks if an array has a certain value and returns true or false base on if the value present in the array. Let change up our example array for the very last time and use integers instead of objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums= [3, 8, 22, 45, 65, 99]
const includesNum = nums.includes(99) 
console.log(includesNum)
// output: true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This function checks to see if 99 is an element in the array. In this case, the output is true. If we change the value to 100, the output will be false because the array does not contain the value 100.&lt;/p&gt;

&lt;p&gt;These are some of the array methods that I find very useful for interviews.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>career</category>
    </item>
  </channel>
</rss>
