{"id":3014,"date":"2019-06-12T00:44:39","date_gmt":"2019-06-12T00:44:39","guid":{"rendered":"http:\/\/softlect.in\/?p=3014"},"modified":"2019-06-15T02:43:17","modified_gmt":"2019-06-15T02:43:17","slug":"javascript-arrays","status":"publish","type":"post","link":"http:\/\/softlect.com\/index.php\/javascript-arrays\/","title":{"rendered":"JavaScript &#8211; Arrays"},"content":{"rendered":"<p><strong>JavaScript Arrays<\/strong><\/p>\n<p>An array is a way of storing list of data<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Defining Arrays<\/strong><\/p>\n<p>Array Constructor<\/p>\n<p>var arrayname = new Array(); creates empty array<\/p>\n<p>var student = new Array(5);\u00a0 creates array with 5 items<\/p>\n<p>It is not necessary to set the length of an array, it can vary as needed during script execution.<\/p>\n<p>Values can be assigned as array is declared<\/p>\n<p>var student = new Array(\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p>Javascript allows arrays to have values of different data types<\/p>\n<p>var answers = new Array(10,\u201dYes\u201d,21.23);<\/p>\n<p>&nbsp;<\/p>\n<p>Arrays Literal Notation<\/p>\n<p>Array literal notation provides a shorter method of defining arrays rather than using constructor.<\/p>\n<p>var student = [];<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d];<\/p>\n<p>var answers = [10,\u201dYes\u201d,21.23];<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Accessing Arrays<\/strong><\/p>\n<p>Array Index is used to access array elements. The array index starts with 0 index.<\/p>\n<p>answer[0] is 10<\/p>\n<p>answer[1] is \u201cYes\u201d<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Length property<\/strong><\/p>\n<p>The length property is used to obtain the length of an array<\/p>\n<p>student.length will give 3<\/p>\n<p>for(var i=0;i&lt;student.length;i++)<\/p>\n<p>Changing Values:<\/p>\n<p>student[1] = \u201czzz\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>Changing Length:<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d];<\/p>\n<p>student.length = 2;<\/p>\n<p>array originally had 3 items but now has 2 items<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d];<\/p>\n<p>student.length = 5;<\/p>\n<p>The array will now have 5 items, and last two items are undefined until values are assigned.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Properties of Arrays<\/strong><\/p>\n<p>constructor<\/p>\n<p>index<\/p>\n<p>length<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Methods of Arrays<\/strong><\/p>\n<p><strong>join()<\/strong> Method<\/p>\n<p>join([separator]);<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d];<\/p>\n<p>var msg = student.join(\u201c,\u201d);\u00a0\u00a0 result aaa,bbb,ccc<\/p>\n<p>&nbsp;<\/p>\n<p><strong>pop()<\/strong> method is used to remove last item from an array, and return last item<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d];<\/p>\n<p>student.pop();<\/p>\n<p>student = [\u201caaa\u201d,\u201dbbb\u201d];<\/p>\n<p>&nbsp;<\/p>\n<p><strong>push()<\/strong> method is used to add items at the end of an array<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d];<\/p>\n<p>student.push(\u201czzz\u201d); student.push(\u201czzz\u201d,\u201dyyy\u201d,\u201dxxx\u201d);<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d,\u201dzzz\u201d];<\/p>\n<p>&nbsp;<\/p>\n<p><strong>shift() method is used to remove first item from an array and return the value<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>unshift() method is used to add items to the beginning of an array<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>reverse() method is used reverse the order of items in an array<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>sort() method converts each array item in to string value and the arrange items<\/strong> in ascending order based on string values<\/p>\n<p>&nbsp;<\/p>\n<p><strong>concat() method is used to concatenate<\/strong><\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d];<\/p>\n<p>var staff = [\u201cxxx\u201d,\u201dyyy\u201d];<\/p>\n<p>var all = student.concat(staff);\u00a0 [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d,\u201dxxx\u201d,\u201dyyy\u201d];<\/p>\n<p>&nbsp;<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d];<\/p>\n<p>var staff = [\u201cxxx\u201d,\u201dyyy\u201d];<\/p>\n<p>var all = student.concat(staff,\u201czzz\u201d,\u201d111\u201d];<\/p>\n<p>result [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d,\u201dxxx\u201d,\u201dyyy\u201d,\u201dzzz\u201d,\u201d111\u201d];<\/p>\n<p>&nbsp;<\/p>\n<p><strong>slice() method is used to slice specific portion of an array<\/strong><\/p>\n<p>arrayname.slice(start,stop); start is index were starts slice and stop is index after the last item you want to slice<\/p>\n<p>&nbsp;<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d,\u201dxxx\u201d,\u201dyyy\u201d];<\/p>\n<p>var result = student.slice(1,3); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 result = [\u201dbbb\u201d,\u201dccc\u201d];<\/p>\n<p>&nbsp;<\/p>\n<p><strong>splice() method removes or replaces items<\/strong><\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d,\u201dxxx\u201d,\u201dyyy\u201d];<\/p>\n<p>student.splice(2,1);\u00a0 result = [\u201caaa\u201d,\u201dbbb\u201d,\u201dxxx\u201d,\u201dyyy\u201d];<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d,\u201dxxx\u201d,\u201dyyy\u201d];<\/p>\n<p>student.splice(2,1,\u201dzzz\u201d);\u00a0 result = [\u201caaa\u201d,\u201dbbb\u201d,\u201dzzz\u201d,\u201dxxx\u201d,\u201dyyy\u201d];<\/p>\n<p>&nbsp;<\/p>\n<p><strong>indexOf() method<\/strong><\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d,\u201dxxx\u201d,\u201dbbb\u201d];<\/p>\n<p>indexOf(\u201cccc\u201d) returns 2<\/p>\n<p>&nbsp;<\/p>\n<p><strong>lastIndexOf()<\/strong> method<\/p>\n<p>var student = [\u201caaa\u201d,\u201dbbb\u201d,\u201dccc\u201d,\u201dxxx\u201d,\u201dbbb\u201d];<\/p>\n<p>student.lastIndexOf(\u201cbbb\u201d) returns 4<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Nesting of Arrays<\/strong><\/p>\n<p>var student = [ \u00a0[\u201caaa\u201d, 60],<\/p>\n<p>[\u201dbbb\u201d, 70],<\/p>\n<p>[\u201dccc\u201d, 80]<\/p>\n<p>];<\/p>\n<p>&nbsp;<\/p>\n<p>var student = new Array(\u00a0 new Array(\u201caaa\u201d, 60),<\/p>\n<p>new Array(\u201dbbb\u201d, 70),<\/p>\n<p>new Array(\u201dccc\u201d, 80)<\/p>\n<p>);<\/p>\n<p>&nbsp;<\/p>\n<p>Accessing<\/p>\n<p>var i = 0;<\/p>\n<p>var j = 0;<\/p>\n<p>for(i=0;i&lt;student.length;i++)<\/p>\n<p>{<\/p>\n<p>for(j=0;j&lt;student[i].length;j++)<\/p>\n<p>{<\/p>\n<p>student[i][j];<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Arrays An array is a way of storing list of data &nbsp; Defining Arrays Array Constructor var arrayname = new Array(); creates empty array var student = new Array(5);\u00a0&hellip; <\/p>\n","protected":false},"author":1,"featured_media":3045,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[78],"tags":[],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3014"}],"collection":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/comments?post=3014"}],"version-history":[{"count":3,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3014\/revisions"}],"predecessor-version":[{"id":3439,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3014\/revisions\/3439"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media\/3045"}],"wp:attachment":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media?parent=3014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/categories?post=3014"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/tags?post=3014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}