CodeBodger's
Home


Javascript Stacks

Stacks are very similar to Arrays, but are indexed from 1 to the length of the stack.
For searches/sorts to operate correctly, the stack should be of similar objects.
  • MyStack = new Stack()
    Instantiates a new stack without a default value.

  • MyStack = new Stack(value/object)
    Instantiates a stack with a default value/object.

  • MyStack.length
    Returns number of items on stack.

  • MyStack.length = integer
    Crops/expands stack to specified length.
    If supplied integer is larger than .length, stack is NOT padded out.

  • MyStack[index]
    Returns value/object at index.

  • MyStack[index] = object/value
    Sets value/object at index to supplied value/object.
    If index = 0, sets default value to supplied value/object.
    If supplied index is larger than .length, stack IS NOT padded out.

  • MyStack.last
    Returns the topmost value/object on the stack.

  • MyStack.last = value/object
    Alters value/object at top of stack to supplied value/object.

  • MyStack.last.property = value/object
    Alters property of top-most stack object to supplied value/object.

  • MyStack.last.property.subproperty = value/object
    Alters subproperty of the property of top-most stack object to supplied value/object.
    Further subproperties can be specified, e.g. 'name.first.initial'.

  • MyStack.push()
    Pushes default value/object onto the top of the stack.

  • MyStack.push(value/object)
    Pushes value/object onto the top of the stack.

  • MyStack.push(value/object, index)
    Pushes value/object into the index position, stack shuffled upwards.
    If supplied index is larger than .length, stack IS padded out.

  • MyStack.pop()
    Returns the value/object on the top of the stack and that value/object is removed from the stack.

  • MyStack.pop(index);
    Returns the value/object at the index position and that value/object is removed from the stack, stack shuffled downwards.

  • MyStack.sortup()
    MyStack is sorted into ascending order of values.

  • MyStack.sortup(.property)
    MyStack is sorted into ascending order according to the values of the property specified. Specify properties and sub-properties thus: '.name.first' (leading period)
    Numeric properties (for arrays, etc) use square brackets thus: '[3]'

  • MyStack.sortdown is identical to .sortup, but sorts into descending order.

  • MyStack.contains(value/object)
    Returns first index of matching value/object or 0 if no match.

  • MyStack.contains(value/object, property)
    Returns first index of object with property matching value/object or 0 if no match.

  • MyStack.search(value/object)
    Returns a stack of all indexes matching value/object or Test.length = 0 if no match.

  • MyStack.contains(value/object, property)
    Returns a stack of all indexes of objects with property matching value/object or Test.length = 0 if no match.

The Code

Just view the source and copy and paste the Stack function out of the head section.