Victory Road Archive

You are viewing an archive of Victory Road.

Victory Road closed on January 8, 2018. Thank you for making us a part of your lives since 2006! Please read this thread for details if you missed it.

Technology → Javascript inArray and removeArrayElement

Page 1 of 1

1. FreezeWarp said on July 4, 2010, 05:41:56 PM (-07:00)

Kyurem
2,186 posts

These are two Javascript functions I wrote for one of my projects. The first checks whether a value is in an array (like PHP's in_array()), while the second removes a value in an array.

Code:
/* Copyright (c) 2010 by Joseph Parsons

 * This file is a part of Fliler.
 * Fliler is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

 * Fliler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License along with Fliler.  If not, see <http://www.gnu.org/licenses/>. */

/* Determines whether a value is in an array.
 * Parameters:
 ** value - The value to check for. Mixed type.
 ** array - The array to check in. Array type.
 * Returns:
 ** True - Element exists in the array.
 ** False - Element does not exist in the array. */
function inArray(value, array) {
  for (var i = 0; i < array.length; i++) {
    if(value == array[i]) return true;
  }
  return false;
}

/* Remove Array Elements
 * Parameters:
 ** value - The value in the array to search for. Mixed type.
 ** array - The array to be searched through. Array type.
 ** all - Whether or not to stop after the first value was removed. Boolean type.
 * Returns: True */
function removeArrayElement(value, array, all) {
  for (var i = 0; i< array.length; i++) {
    if (value == array[i]) {
      array.splice(i,1);
      if (!all) return true;
    }
  }
  return true;
}

Page 1 of 1

User List - Contact - Privacy Statement - Lycanroc.Net