<?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: lokender singh</title>
    <description>The latest articles on DEV Community by lokender singh (@lokinder1).</description>
    <link>https://dev.to/lokinder1</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F377629%2F3a4f6f89-61db-46bb-aa62-c704e0e02c86.png</url>
      <title>DEV Community: lokender singh</title>
      <link>https://dev.to/lokinder1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lokinder1"/>
    <language>en</language>
    <item>
      <title>CheatSheet Of Most Useful JavaScript Array Functions</title>
      <dc:creator>lokender singh</dc:creator>
      <pubDate>Tue, 14 Jul 2020 15:03:02 +0000</pubDate>
      <link>https://dev.to/lokinder1/cheatsheet-of-most-useful-javascript-array-functions-48j1</link>
      <guid>https://dev.to/lokinder1/cheatsheet-of-most-useful-javascript-array-functions-48j1</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;As a professional developer, most of my time I have to play with arrays to perform manipulation in data to get the desired outcome.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;To play with arrays very well we must have to remember inbuilt array functions, So I make a list of arrays which I use most of my time.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  These methods are the most used ones, they cover 99% of use cases
&lt;/h3&gt;




&lt;h4&gt;
  
  
  To add/remove elements:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Array.push(...items)&lt;/code&gt; – adds items to the end,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4]
array.push(10) // 5 (push returns the length of the new array)
// array = [1, 2, 3, 4, 10]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.pop()&lt;/code&gt; – extracts an item from the end,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3 , 4]
array.pop() // 4 (pop returns the element removed)
// array = [1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.shift()&lt;/code&gt; – extracts an item from the beginning,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4]
array.shift() // 1(shift returns the removed element)
// array = [2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.unshift(...items)&lt;/code&gt; – adds items to the beginning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4]
array.unshift(9, 10) // 6 (unshift returns the length of new array)
// array = [9, 10, 1, 2, 3, 4] 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.splice(pos, deleteCount, ...items)&lt;/code&gt; – at index pos delete deleteCount elements and insert items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.slice(start, end)&lt;/code&gt; – creates a new array, copies elements from position start till end (not inclusive) into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4]
const slicedArray = array.slice(0, 2)
// array = [1, 2, 3, 4]
// slicedArray = [1, 2]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.concat(...items)&lt;/code&gt; – returns a new array: copies all members of the current one and adds items to it. If any of items is an array, then its elements are taken.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4]
const concatArray = array.concat([5, 6, 7, 8])
// array = [1, 2, 3, 4]
// concatArray = [1, 2, 3, 4, 5, 6, 7, 8]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;h4&gt;
  
  
  To search among elements:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Array.indexOf/lastIndexOf(item, pos)&lt;/code&gt; – look for item starting from position pos, return the index or -1 if not found.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));
// expected output: 1

const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];

console.log(animals.lastIndexOf('Dodo'));
// expected output: 3

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.includes(value)&lt;/code&gt; – returns true if the array has value, otherwise false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.find/filter(func)&lt;/code&gt; – filter elements through the function, return first/all values that make it return true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4]
const filteredArray = array.filter(element =&amp;gt; element%2)
// array = [1, 2, 3, 4]
// filteredArray = [1, 3]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.findIndex(func)&lt;/code&gt; - like find, but returns the index instead of a value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) =&amp;gt; element &amp;gt; 13;

console.log(array1.findIndex(isLargeNumber));
// expected output: 3

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;h4&gt;
  
  
  To iterate over elements:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Array.forEach(func)&lt;/code&gt; – calls func for every element, does not return anything.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4]
array.forEach((element, index) =&amp;gt; {
   console.log(`Element ${element} at index ${index}`)
})

\\ Element 1 at index 0
\\ Element 2 at index 1
\\ Element 3 at index 2
\\ Element 4 at index 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  To transform the array:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Array.map(func)&lt;/code&gt; – creates a new array from results of calling func for every element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4]
const mapArray = array.map(element =&amp;gt; element * 2)
// array = [1, 2, 3, 4]
// mapArray = [2, 4, 6, 8]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.sort(func)&lt;/code&gt; – sorts the array in-place, then returns it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.reverse()&lt;/code&gt; – reverses the array in-place, then returns it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = ['one', 'two', 'three'];
const reversed = array1.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.split()/Array.join()&lt;/code&gt; – convert a string to array and back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

const string = "Fire,Air,Water";
console.log(string.split(','));
// output: ["Fire", "Air", "Water"] 

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.reduce(func, initial)&lt;/code&gt; – calculate a single value over the array by calling func for each element and passing an intermediate result between the calls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4]
const result = array.reduce((accumulator, current) =&amp;gt; (
   accumulator + current
), 10)
// array = [1, 2, 3, 4]
// result = 20
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;h4&gt;
  
  
  Additionally:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Array.isArray(arr)&lt;/code&gt; - checks arr for being an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.isArray([1, 2, 3]);  // true
Array.isArray({foo: 123}); // false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.keys()&lt;/code&gt; - returns a new Array Iterator object that contains the keys for each index in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();

for (const key of iterator) {
  console.log(key);
}

// expected output: 0
// expected output: 1
// expected output: 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.values()&lt;/code&gt; -  returns a new Array Iterator object that contains the values for each index in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = ['a', 'b', 'c'];
const iterator = array1.values();

for (const value of iterator) {
  console.log(value);
}

// expected output: "a"
// expected output: "b"
// expected output: "c"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.some()&lt;/code&gt; - at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) =&amp;gt; element % 2 === 0;

console.log(array.some(even));
// expected output: true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.every()&lt;/code&gt; - all elements in the array pass the test implemented by the provided function. It returns a Boolean value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const isBelowThreshold = (currentValue) =&amp;gt; currentValue &amp;lt; 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// expected output: true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>codequality</category>
      <category>node</category>
    </item>
  </channel>
</rss>
