Arrays Part_02 ============== Reading of elements of an array: ================================ using methods ============= 1) toString(): ============== ==> a built-in function in JS can be used to convert an array into string. Syntax: array-name.toString() let sales = [56000, 39995, 43398, 19796, 97890, 59987]; console.log("The Array with Sales (Original Array) = ",sales); // convert the array into string sales_result = sales.toString(); console.log("The Array in String Format = ",sales_result); console.log("The Type = ",typeof sales_result); console.log("The Type of definition = ",typeof sales); 2) join(): ========== ==> when the given array need to convert into the string by joining all the individual elements of an array with the specified delimiter (separator), we can use "join()". Syntax: array-name.join('separator') let sales = [56000, 39995, 43398, 19796, 97890, 59987]; console.log("The Array with Sales (Original Array) = ",sales); // joining of array elements into string with specified separator. sales_join = sales.join('-->'); console.log("The Given Array with separator = ",sales_join); console.log("The Type of Result = ",typeof sales_join); 3) slice(): =========== ==> slice() is a built-in function which can be used to acquire the part of the array (from the specified index range). Syntax: array-name.slice(start-index, end-index) Here: from end-index: while the slicing we can reduce the one let sales = [56000, 39995, 43398, 19796, 97890, 59987]; console.log("The Array with Sales (Original Array) = ",sales); res1 = sales.slice(2); // from index 2 till the end all elements can acquire. res2 = sales.slice(1,5); // acquire the array elements with index 1, 2, 3, 4 console.log("Result1 = ",res1); console.log("Result2 = ",res2); console.log("The Type of result1 = ",typeof res1); console.log("The Type of result2 = ",typeof res2); 4) filter(): ============ ==> can create new array object with elements which are based on the specified condition. ==> the condition for filter() must be define with another function. Syntax: array-name.filter(function(){// logic}); let sales = [56000, 39995, 43398, 19796, 97890, 59987]; console.log("The Array with Sales (Original Array) = ",sales); // filter condition function getData(value){ return value > 50000; } filtered_array = sales.filter(getData); console.log("The Array after the filter = ",filtered_array); resultant_array = sales.filter(function(sale){return sale < 50000}); console.log("The Resultant Array = ",resultant_array); 5) find(): ========= ==> based on the condition it can return the first occurred element from the given array. Syntax: array-name.find(function(){//logic}); let sales = ['56000', '39995', 43398, 19796, 97890, 59987]; console.log("The Array with Sales (Original Array) = ",sales); // filtered condition function getData(value){ return value > 50000; } let res1 = sales.find(getData); console.log(res1); console.log(typeof res1); let res2 = sales.find(function(data){return data < 50000}); console.log(res2); console.log(typeof res2); 6) map() ======== Syntax: array-name.map(function(){//logic}); let sales = ['56000', '39995', 43398, 19796, 97890, 59987]; console.log("The Array with Sales (Original Array) = ",sales); console.log("All array elements = "); sales.map(function(value){ document.write(`
  • ${value}
  • `); }) ===================================================== using loops =========== for loop while loop do while loop for loop ======== Syntax: for(var iteration-variable = 0;iteration-variable < array.length;++iterator-variable){ //logic } let sales = ['56000', '39995', 43398, 19796, 97890, 59987]; console.log("The Array Elements = "); // forward accessing for(var sale = 0;sale < sales.length;++sale) { console.log(sales[sale]); } console.log(); // reverse Accessing for(var s = sales.length - 1;s >= 0;--s){ console.log(sales[s]); } while loop: =========== Syntax: initialization = 0 while(initialization < array.lenth){ //logic // update } let sales = ['56000', '39995', 43398, 19796, 97890, 59987]; console.log("The Array Elements = "); // forward accessing let i = 0; while(i < sales.length){ console.log(sales[i]); i++; } console.log(); // reverse Accessing j = sales.length - 1; while(j >= 0){ console.log(sales[j]); j--; } =================================================== using iterators =============== two different iterators 1) for-in ========= an iterator used to access/read only the properties of array. ==> to get values using for-in, we should use the [] let sales = ['56000', '39995', 43398, 19796, 97890, 59987]; console.log("The Array Elements = "); for(var s in sales){ console.log(`${s} --> ${sales[s]}`); } 2) for-of ========== is an iterator can use to access the array element values. let sales = ['56000', '39995', 43398, 19796, 97890, 59987]; console.log("The Array Elements = "); for(var s of sales){ console.log(s); }