Arrays Flashcards
What is the method used to merge two or more arrays?
Does it create an new array ?
concat() . Yes it creates an new array
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"]
What is Array.prototype.entries()
The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
//USING for…of
for(let [idx, item] of a.entries()){ // 0 ‘a’
console.log(idx,item) // 1 ‘b’
} // 2 ‘c’
for(let x of a.entries()) { //prints [ 0, ‘a’ ]
console.log(x) // [ 1, ‘b’ ]
} // [ 2, ‘c’ ]
What is the purpose of every() function from Array ? Does the loop stop if callback condition returns falsy ?
The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value. YES, it won’t iterate over remaining elements once the condition is false
[If once condition fails . i.e falsy , it won’t check for other elements in array i.e the for loop breaks]
**the callback function is executed once for each element present in the array until it finds the one where callbackFn returns a falsy value. If such an element is found, the every method immediately returns false. Otherwise, if callbackFn returns a truthy value for all elements, every returns true.
const a = [‘10’, ‘1’, ‘3’];
const isGreaterThan1 =(num, idx, ar)=>{ console.log(num , idx, ar) return num > 1 } a.every(isGreaterThan1)
What is filter() function in Array? Does it create a new array? Does it iterate over undefined values in an array.
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
const arr = ['10','20', '20','30'] const newArray = arr.filter((num, idx, arr)=>{ return num > 20 })
eg. if we have [ ‘10’, ‘20’, ‘20’, ‘30’, <16 empty items> ] array. filter() function will only iterate over 4 elements and skip remaining 16 empty items
what is find function? Does it return all the matched values that satisfies the testing function?
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. NO, find function only returns the ONE AND FIRST SATISFIED value
const arr = [‘10’,’20’, ‘20’,’30’]
const value = arr.find((num, idx, a)=>{ return num > 5 })
console.log(value) // prints 10. Prints the First match based on callback testing func
What is the difference between find and filter function
- find iterates over each element, even if the values are undefined
- the return type is a array element in find func
- filter does not iterate over undefined values or uninitialised values
- the return type is a new array in filter func
eg.
const arr = [‘10’,’20’]
arr.length = 10 //[ ‘10’, ‘20’, <8 empty items> ]
const value = arr.find((num, idx, a)=>{ console.log('each find ', num, idx) //will be called 10 times return num > 100 })
console.log(value)
The find method executes the callbackFn function once for each index of the array until the callbackFn returns a truthy value. If so, find immediately returns the value of that element. Otherwise, find returns undefined.
How to use forEach function on arrays
It is called on an array
const arr = ['10', '20'] arr.length = 10
arr.forEach((num)=>{
console.log(num)
})
What are important facts about includes function
Determines whether an array includes a given value among its entries, and true or false accordingly.
When comparing strings and characters, includes() is case-sensitive.
eg 1 const animals = ['Lion', 'Tiger', 'Frog'] animals.includes('frog') //returns false
eg: 2 const array1 = [1, 2, 3]; console.log(array1.includes(2)); // returns true
eg:3 const arr = [10, 10, -0, 20, 40, 0] arr.includes(-0, 3) //returns true.
We are search for -0 from index 3, the includes method returns true because -0 is considered to be equal to both 0 and +0.
But false is not equal to 0
When to use join func
The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
const arr = [“door”, “door”, “window” ,”window”]
arr. join( ‘/’ ) //’door/door/window/window’
arr. join( ‘|’ ) //’door|door|window|window’
arr. join( ‘.’ ). //’door.door.window.window’
What is keys() function. How iteration is different than array.entries()
The keys() method returns a new Array Iterator object that contains the keys for each index in the array.
IT DOES NOT IGNORE HOLES/UNDEFINED/UNINITIALIZED VALUES
const arr = [“10”, “20”, “50” ,”100”]
for(let key of arr.keys()){
console.log(key)
}
In keys(), we cannot use square bracket inside for for(let [key] of arr.keys()){ //Error console.log(key) }
What is map function? When not to use it ? Does it create a new array
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. The callback function is not called for missing elements of the array;
that is: (this is different than undefined) I DONT UNDERSTAND THIS AS OF NOW . IGNORE ??
indexes that have never been set;
indexes which have been deleted.
//callback is getting called for empty items also const arr = [10, 20, 50 , 100] arr.length = 10 //[ 10, 20, 50, 100, <6 empty items> ] const mappedArr = arr.map((each)=>{ return each * 2 } ) mappedArr // [ 20, 40, 100, 200, <6 empty items> ]
When not to use map func?
you’re not using the array it returns; and/or
you’re not returning a value from the callback.
Use forEach or for…of instead
YES, map func creates a new array
reverse func in JS. Is it in place? Does it create a new array
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first. It modifies the input array
const arr = [10, 20, 50 , 100]
arr. reverse() // [ 100, 50, 20, 10 ]
console. log(arr) //[ 100, 50, 20, 10 ]
slice function ?
It returns a shallow copy of elements from the original array. slice does not alter the original array.
Elements of the original array are copied into the returned array as follows:
For object slice copies object references into the new array. Both the original and new array refer to the same object. If an object changes, the changes are visible to both the new and original arrays.
For strings, numbers and booleans (not String, Number and Boolean objects), slice copies the values into the new array. Changes to the string, number, or boolean in one array do not affect the other array.
If a new element is added to either array, the other array is not affected
what is splice. DOES IT MODIFY THE ARRAY ?
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, see slice().
eg. let myCar = [ 'red', '1997', "delhi"]
const result = myCar.splice(0,1);
console. log(result)// ‘red’
console. log(myCar) // [‘1997’, ‘delhi’]
It modified the original array.
Replace let myCar = ['cherry condition', '1997', "delhi", "TS"]
//remove 1 element starting from index 2 and insert ‘Winter’ and ‘Summer’ at index 2
const result = myCar.splice(2, 1, ‘Winter’, ‘summer’);
console. log(result) // [ ‘delhi’ ]
console. log(myCar) //[ ‘cherry condition’, ‘1997’, ‘Winter’, ‘summer’, ‘TS’ ]
slice vs splice.
P in splice is Pagal. Pagal is very destructive
Slice
mutation : does not mutate
arguments : start, end
use. : make shallow copy of the original array return output : returns the copied array
Splice
mutation : mutates
arguments : start, delete count, n items to replace with
return output : add or remove from the original array returns removed elements i