Array

Create Empty Array with sizes

const arr = [...Array(3)];
arr.forEach((val)=>{console.log(val);});
// undefined
// undefined
// undefined

// Generate a sequence of numbers
// Since the array is initialized with `undefined` on each position,
// the value of `v` below will be `undefined`
Array.from({ length: 5 }, (v, i) => i);
// [0, 1, 2, 3, 4]

Filter and Find

  • Get the ideal value based on the condition

  • Find is used to get the first ideal value from the array

  • Filter is used to return new array that is ideal based on the condition

Splice

  • Insert new value into original array on specific position and delete certain number of array on specific location

For Each and Map

Common

  • Both can use to run void function and have the same output

Difference

  • For Each focus on affecting the original array, while map focus on creating new array

Conclusion

  • When doing logic on original array, use for Each

  • When creating new array instead of affecting original array, use map

Sort

Includes

  • arr.includes(valueToFind , fromIndex) and return boolean

  • if from Index is smaller than 0, for example: -1 . the from Index will be the size of array - 1

Reduce

  • Example 1:

  • Example 2

Checking

Concat and join

Keys and values

FlatMap

Last updated

Was this helpful?