Array

Basics

var fruits = ['Apple', 'Banana'];

var last = fruits[fruits.length - 1];
  • .length

  • .push(e)

  • .pop()

  • .shift() - popleft returning first ele

  • .unshift(e) - pushes to array returning length

  • .indexOf(e) - return index of ele or -1

  • .includes(e) - return true/false

  • .findIndex(y => y === 1) - returns the first index which cause ft to return true

  • .sort() - Takes ft (a,b) =>

    • < 0 — a b4 b

    • > 0  — b b4 a

    • = 0  — a and b unchanged

  • .reverse() - return new???

Functional Stuff

  • .map(ft/1) - return new array with ft applied to each

  • flatMap(ft/1) - map followed by a depth 1 flat() , allows modification of number of elements

  • .reduce(ft(acc, e), initialValue?) - return single ele..

  • .some(ft/1) - true if one ele satisfies ft given

  • .filter(ft/1)- return new array with elements where ft true

words.filter(word => word.length > 6);
words.map((element, index) => element+index);
arr.reduce((acc, x) => acc + x, 0) //sum
j.reduce((acc, a) => acc + (a/j.length), 0) //average

//when you want to find a specific entry
[1,2,3].filter(x => x === 3).reduce((arr, cur) => cur, null); //3
[1,2,3].filter(x => x === 4).reduce((arr, cur) => cur, null); //null
[1,2,3].reduce((arr, cur) => arr || cur, null) //first nonnull value so 1

Can do async fts in map and then wrap it in a promise.all

Advanced

Subarr

.slice for subarrs (start, end not included)

.splice([start index], [number beyond start], [item to insert opt]) for removing&saving section

.concat combine two arrays

const removedItem = fruits.splice(pos, 1); // this is how to remove a single item at pos

fruit.splice(3, 0, "apple"); //insert at index 3

fruits.splice(fruits.indexOf("banana"), 1);

fruits.concat(["other", "mango"])

copy

const fruitCopy = fruits.slice(); 

For each

Executes a callback ft for each ele, returns undefined

Beware async stuff

array.forEach(callback[, thisArg])

array1.forEach(function(element) {
  console.log(element);
});

Queue

  • .push(e)

  • .pop()

  • .shift(e) => pop from beginning

  • .unshift() => add to beginning

Is Array

Array.isArray([1,2]);// true;

Last updated