#8 Javascript Arrays | Javascript Hindi Course For Beginners ( 2023 )
An array in JavaScript is a data structure that allows you to store multiple values in a single variable. It's like a list of items, where each item can be accessed by its index (position) in the list. The items in an array can be of any data type, including numbers, strings, objects, and other arrays. Arrays are a useful tool for organising and manipulating data in a program.
// Example of Array let daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; //Array is index value pair. // index of array start from 0 // we can also write array as object let daysOfWeek = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday' }; // Inside array we can store any data we want. like boolean , string // number , array ,objects
CRUD Operation Array
// CREATE let daysOfWeek = [] daysOfWeek.push('Sunday') // add at end daysOfWeek.push('Monday') daysOfWeek.unshift("START1","START2" ) // add at starting // So on ....... // READ let daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; const sunday = daysOfWeek[0] // UPDATE daysOfWeek[0] = "Unkown" // DELETE daysOfWeek.pop() // removes last element -> FRIDAY daysOfWeek.shift() // removes first element -> Sunday daysOfWeek.splice(3, 1); // removes the element at index 3 (Wednesday) daysOfWeek.slice(2); // removes first two elements // but does not modify same array // removes array elements which don't fit in condition const filteredDay = daysOfWeek.filter(word => word.length > 6);
How loop over array
let daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; // Simple for loop for (let i = 0; i < daysOfWeek.length; i++) { let day = daysOfWeek[i]; console.log(`Today is ${day}`); } // Using for of loop for (let day of daysOfWeek) { console.log(`Today is ${day}`); } // using forEach loop -> forEach loop does't writtern any array // there are three parameters -> value , currentIndex , fullArray daysOfWeek.forEach((day,index,array) => { console.log(`Today is ${day}`); console.log(`Day ${index}`); console.log(array); }); // using map loop -> map loop return new array let messages = daysOfWeek.map(day => `Today is ${day}`); console.log(messages);
Array Methods
// Array.prototype.find() // For Searching element in array const array1 = [5, 12, 8, 130, 44]; const found = array1.find(element => element > 10); console.log(found); // Expected output: 12
// Array.prototype.indexOf() // For search index of element const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; console.log(beasts.indexOf('bison')); // Expected output: 1 // Start from index 2 console.log(beasts.indexOf('bison', 2)); // Expected output: 4 console.log(beasts.indexOf('giraffe')); // Expected output: -1
// Array.prototype.findIndex() // // For search index of element based on condiiton const array1 = [5, 12, 8, 130, 44]; const isLargeNumber = (element) => element > 13; console.log(array1.findIndex(isLargeNumber)); // Expected output: 3
// Array.prototype.concat() // for combining two arrays const array1 = ['a', 'b', 'c']; const array2 = ['d', 'e', 'f']; const array3 = array1.concat(array2); console.log(array3); // Expected output: Array ["a", "b", "c", "d", "e", "f"]
// Array.prototype.at() // getting value of element using index of element const array1 = [5, 12, 8, 130, 44]; let index = 2; console.log(`Using an index of ${index} the item returned is ${array1.at(index)}`); // Expected output: "Using an index of 2 the item returned is 8" index = -2; console.log(`Using an index of ${index} item returned is ${array1.at(index)}`); // Expected output: "Using an index of -2 item returned is 130"
Array Checker Methods
// Array.prototype.every() // every condition is true then it will return true // otherwise false const isBelowThreshold = (currentValue) => currentValue < 40; const array1 = [1, 30, 39, 29, 10, 13]; console.log(array1.every(isBelowThreshold)); // Expected output: true // Array.isArray() // For checking current variable is object or not console.log(Array.isArray([1, 3, 5])); // Expected output: true console.log(Array.isArray('[]')); // Expected output: false // Array.prototype.some() // for some element will safisfy condition then it will return true const array = [1, 2, 3, 4, 5]; // Checks whether an element is even const even = (element) => element % 2 === 0; console.log(array.some(even)); // Expected output: true // Array.prototype.includes() // if elements exist inside array then it will return true const array1 = [1, 2, 3]; console.log(array1.includes(2)); // Expected output: true const pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // Expected output: true console.log(pets.includes('at')); // Expected output: false
// Array.prototype.reduce() // it will combine array // based on some operation const array1 = [1, 2, 3, 4]; // 0 + 1 + 2 + 3 + 4 const initialValue = 0; const sumWithInitial = array1.reduce( (accumulator, currentValue) => accumulator + currentValue, initialValue ); console.log(sumWithInitial); // Expected output: 10
// Array.prototype.sort() // for sorting using this method const months = ['March', 'Jan', 'Feb', 'Dec']; months.sort(); console.log(months); // Expected output: Array ["Dec", "Feb", "Jan", "March"] const array1 = [1, 30, 4, 21, 100000]; array1.sort(); console.log(array1); // Expected output: Array [1, 100000, 21, 30, 4]
// Array.prototype.join() // it will combine elements together const elements = ['Fire', 'Air', 'Water']; console.log(elements.join()); // Expected output: "Fire,Air,Water" console.log(elements.join('')); // Expected output: "FireAirWater" console.log(elements.join('-')); // Expected output: "Fire-Air-Water" // Array.prototype.reverse() // it will just reverse order of elements const array1 = ['one', 'two', 'three']; console.log('array1:', array1); // Expected output: "array1:" Array ["one", "two", "three"]
Questions
question 1
Concatenate Variable Number of Input Arrays
Create a function that concatenates n input arrays, where n is variable.
@helen_yu
// examples concat([1, 2, 3], [4, 5], [6, 7]) ➞ [1, 2, 3, 4, 5, 6, 7] concat([1], [2], [3], [4], [5], [6], [7]) ➞ [1, 2, 3, 4, 5, 6, 7] concat([1, 2], [3, 4]) ➞ [1, 2, 3, 4] concat([4, 4, 4, 4, 4]) ➞ [4, 4, 4, 4, 4]
Question 2
Store Payment
Given a total due and an array representing the amount of change in your pocket.
determine whether or not you are able to pay for the item.
Change will always be represented in the following order: ₹1 coin, ₹2 coin, ₹5 coin, ₹10 coin
changeEnough([25, 20, 5, 5], 120)
should yield true
@helen_yu
since having 25 → ₹1 coin, 20 → ₹2 coin, 5 → ₹5 coin and 5 → ₹10 coin
gives you 25 + 40 + 25 + 50 = 140
//examples changeEnough([10, 6, 5, 2], 69) ➞ false changeEnough([20, 10, 10, 5], 100) ➞ true
Question 3
Factorial of number using reduce method
factorial of 4 = 1*2*3*4 = 24
// examples factorial(1) -> 1 factorial(2) -> 2 factorial(3) -> 6 factorial(4) -> 24
Question 4
Find the Second Largest Number
Create a function that takes an array of numbers and returns the second largest
number. @matt
// examples secondLargest([10, 40, 30, 20, 50]) ➞ 40 secondLargest([25, 143, 89, 13, 105]) ➞ 105 secondLargest([54, 23, 11, 17, 10]) ➞ 23
Javascript Arrays | Javascript Hindi Course For Beginners ( 2023 ) Complete Notes of JS - https://dosomecoding.com/courses/javascript/javascript-hindi-course-for-beginners Javascript Beginners Playlist Link - https://www.youtube.com/playlist?list=PLPppPPmk0i3gZCY8JZ0H5oykFGevvNzNS HTML Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gL2isb9Kr1GvTM8id2gdtY CSS Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gWK5TVILnKSvuc9Fc15sbH Html and CSS practice Projects https://www.youtube.com/playlist?list=PLPppPPmk0i3hZCNmbVtcP1hlwDKOdUFX9 Javascript Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gZCY8JZ0H5oykFGevvNzNS Linkedin - https://linkedin.com/in/anshuopinion Telegram Channel - https://telegram.me/dosomecodinghelp Instagram - https://instagram.com/dosomecoding Github - https://github.com/anshuopinion