728x90
반응형

Converting an Array to a String

.toString() : converts an array to a string of (comma separated) array values.

Looping Array Elements

.forEach(Function);

Adding Array Elements

.push(value) : add a new element to an array  or array[array.length] = "" 

new Array()

ex)new Array( value , value);    new Array(position);

How to Recognize an Array

Array.isArray(fruits); or (fruits instanceof Array);

Array Methods

.length :  returns the length (size) of an array

.toString() : converts an array to a string of (comma separated) array values

.at(position);  eturns an indexed element from an array. == array[position]

.join(" * ") :  like toString()  but in addition you can specify the separator

.pop()  : removes the last element from an array

.push(value) : adds a new element to an array (at the end)

.shift() : returns the value that was "shifted out"

.unshift(value) :   adds a new element to an array (at the beginning), and "unshifts" older elements. like array[0] = value

 

Array delete()

Using delete() leaves undefined holes in the array.

Use pop() or shift() instead.

Merging Arrays (Concatenating)

.concat(array) : creates a new array by merging (concatenating) existing arrays

Array copyWithin()

.copyWithin(position, copyValuePosition) : copies array elements to another position in an array

.copyWithin(position , copyValuePositionStartIndex , copyValuePositionEndIndex )  : does not change the length of the array.

Array flat()

const myArr = [[1,2],[3,4],[5,6]];   : creates a new array with sub-array elements concatenated to a specified depth.
const newArr = myArr.flat();

const myArr = [1, 2, 3, 4, 5, 6]; :  first maps all elements of an array and then creates a new array by flattening the array.
const newArr = myArr.flatMap(x => [x, x * 10]);

Splicing and Slicing Arrays

.splice(addPosition, removecount, addvalue, addvalue1......); : can be used to add new items to an array

.toSpliced(removePosition, removecount) :  safe way to splice an array without altering the original array.

 new toSpliced() method and the old splice() method is that the new method creates a new array, keeping the original array unchanged, while the old method altered the original array

.slice(removeIndex) :  slices out a piece of an array into a new array ( does not remove any elements from the source array. )

 

Array Search

 Array indexOf()

.indexOf("value")  : searches an array for an element value and returns its position.

.indexOf(value, start) : start at the given position counting from the end, and search to the end

 returns -1 if the item is not found.

.lastIndexOf(value)  : returns the position of the last occurrence of the specified element

.lastIndexOf(item, start) : start at the given position counting from the end, and search to the beginning

 

.includes(value) : to check if an element is present in an array( including NaN, unlike indexOf )

Array find()

.find(myFunction) : returns the value of the first array element that passes a test function

.findIndex(myFunction); :  returns the index of the first array element that passes a test function

.findLast(x => x > 40); : start from the end of an array and return the value of the first element that satisfies a condition

.findLastIndex(x => x > 40) : finds the index of the last element that satisfies a condition.

 

Sorting an Array

.sort(); : sorts an array alphabetically

.toSorted();  : as a safe way to sort an array without altering the original array

.reverse()  : reverses the elements in an array

.toReversed() : as a safe way to reverse an array without altering the original array.

 

Numeric Sort

sort() function sorts values as strings.

.sort(function(a, b){return a - b});

.sort(function(a, b){return b - a});

.sort(function(){return 0.5 - Math.random()});

Using Math.min() or Math.max() on an Array

Math.max.apply(null, arr)  : == Math.min(arr

Math.min.apply(null, arr) : == Math.max(arr

Array forEach()

.forEach( function) : calls a function (a callback function) once for each array element

Array map()

.map(function)

.flatMap(function) : 

Array filter()

.filter(function) : creates a new array with array elements that pass a test.

ex )  function parameter : value, index, array.

  • The item value
  • The item index
  • The array itself

Array reduce()

.reduce(myFunction) :  a function on each array element to produce (reduce it to) a single value.

.reduce(myFunction, number);

.reduceRight(myFunction) :   from right-to-left in the array.

  • The total (the initial value / previously returned value)
  • The item value
  • The item index
  • The array itself

Array every() , some()

.every(myFunction);  :  checks if all array values pass a test.

.some(myFunction); : checks if some array values pass a test.

  • The item value
  • The item index
  • The array itself

Array.from()

Array.from(String) :  returns an Array object from any object with a length property or any iterable object.

Array keys()

.keys(); : returns an Array Iterator object with the keys of an array.

Array entries()

.entries(); :  returns an Array Iterator object with key/value pairs

Array with() Method

with() method as a safe way to update elements in an array without altering the original array.

ex) = months.with(2, "March"); : replace the index 2 value  

Array Spread (...)

ex) const year = [...q1, ...q2, ...q3, ...q4];   :  ... operator expands an iterable (like an array) into more elements:

 

728x90
반응형

'JAVASCRIPT' 카테고리의 다른 글

20241122_2 Math Object  (0) 2024.11.22
20241122_1 Date Objects  (0) 2024.11.22
20241121_1 Numbers  (2) 2024.11.21
20241120_ String (Methods,Templates)  (0) 2024.11.20
20241120_2 기초2  (2) 2024.11.20

+ Recent posts