The
Palm
Civet

Posts tagged javascript

javascript array shortcut functions

jquery is tight, but when you have to resort to plain old javascript, it can be frustrating that a lot of common tasks are not dead simple or elegant. here are some syntactical shortcuts for working with objects in arrays =>


// add an object to an array
function addObj(arr,obj) {
  arr.push(obj);
  return arr;
}

// remove an object from array
function removeObj(arr,obj) {
  arr.splice(arr.indexOf(obj), 1);
  return arr;
}

// check if object is included in array
function include(arr, obj) {
  for(var i=0; i=<arr.length; i++) {
    if (arr[i] == obj) return true;
  }
}

compiling and sharing code, ideas, and tools for making better websites and applications.

by justin talbott {email me}

what is a palm civet?