JavaScript: Data Structures

Array

Array: a special type of Javascript object with the as keys automatically allocated as numeric strings, the first being “0” followed by “1” and so on. Note the keys are strings. There is also a length property.

Let myArray = [“A”,”B”,”C”];

// similar to an object

myArraySimilar = {“0”:“A”,
                  ”1”:”B”,
                  ”2”:”C”,
                  ”length”:3};

‘length’ can be overridden. If smaller than previous value, data will be dropped. If larger, the array will be expanded and filled with “undefined” (“missing value”).

Delete myArray[1] 

sets the second value to undefined / “missing value”. The key remains in the array.

Array Manipulation

myArray.push(newValue);adds value to end of the array
myArray.pop();removes value from the end of the array and returns it
myArray.shift();removes first value from the array
myArray.unshift(addFirst);adds new value to start of array
myArray.reverse();reverse elements
myArray.splice(startLoc, numToRemove, …insertElems)can truncate, remove or replace elems, a negative start location starts from the end
myArray.sort(compareFunc);e.g. myArray.sort( (x,y) => x – y )
Beware: without ‘compareFunc’ defaults to string comparison: 100 less than 2. Good choice (x,y) => x.localeCompare(y)
myArray.flat();flattens the array, i.e. “explodes” the elements that are arrays themselves

Array Search

Item searches use strict equality (===) so 3 and “3” are not equivalent. If no item is found on an index search, -1 is returned.

  • myArray.includes(item)
  • myArray.indexOf(item)
  • myArray.lastIndexOf(item)
  • myArray.findIndex(condition function);
  • myArray.find(condition function);→ returns first element that meets condition
  • myArray.every(condition function); → returns true if every element is true
  • myArray.some(condition function); → returns true if at least one element is true
  • myArray.filter(condition function); → returns array of items where condition is true

Condition Function: (eachViewedValueInTurn, indexOfValue, wholeArray) => yourTestLogic

For example:

x => x** 2== 9  // values squared that are 9 → [3, -3]

(x,y,z) => (x** 2== 9) && (y > 3)  // values squared that are 9 in positions after index 3

Set

Sets hold a list of values with fast look-up.

const mySet = new Set();
const mySet = new Set([ 1, 2, 3, 4, 5]);
mySet.add(9);
mySet.delete(2);
mySet.has(9); // true
mySet.has(2); // false

Map

Maps hold a fast look-up between a key and a value.

const myMap = new Map();
const myMap = new Map( [ [1, “one”],   [2,”two”]  ] );
myMap.set(3, “three”);
myMap.delete(2);
myMap.has(3); // true
myMap.has(2); // false
myMap.get(3); // → “three”

Items held in order inserted. Hash function (fast look-up) not customisable so care is required with the key to avoid duplicates.

Other Data Structures

WeakSet & WeakMap are available where garbage collection is possible if the items are not used elsewhere.

Numeric arrays are available for dealing with numeric data:

  • Signed integers: Int8Array, Int16Array, Int32Array
  • Unsigned integers: Uint8Array, Uint16Array, Uint32Array
  • Float: Float32Array, Float64Array

ArrayBuffer is available for byte sequences such as files of web input

const myBuff = new ArrayBuffer(size);
const view = new DataView(myBuff);
view.getUint32(...)