Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var array = [2, 5, 9];
- var index = array.indexOf(5);
- if (index > -1) {
- array.splice(index, 1);
- }
- array.splice(i, 1);
- for(var i = array.length - 1; i >= 0; i--) {
- if(array[i] === number) {
- array.splice(i, 1);
- }
- }
- delete array[i];
- delete array[ index ];
- array.splice( index, 1 );
- var value = array.splice( index, 1 )[0];
- function remove(arr, item) {
- for(var i = arr.length; i--;) {
- if(arr[i] === item) {
- arr.splice(i, 1);
- }
- }
- }
- // Extending Array prototype with new function,
- // if that function is already defined in "Array.prototype",
- // then "Object.defineProperty" will throw an exception
- Object.defineProperty(Array.prototype, "stackoverflow_remove", {
- // Specify "enumerable" as "false" to prevent function enumeration
- enumerable: false,
- /**
- * Removes all occurence of specified item from array
- * @this Array
- * @param itemToRemove Item to remove from array
- * @returns {Number} Count of removed items
- */
- value: function (itemToRemove) {
- // Count of removed items
- var removeCounter = 0;
- // Iterate every array item
- for (var index = 0; index < this.length; index++) {
- // If current array item equals itemToRemove then
- if (this[index] === itemToRemove) {
- // Remove array item at current index
- this.splice(index, 1);
- // Increment count of removed items
- removeCounter++;
- // Decrement index to iterate current position
- // one more time, because we just removed item
- // that occupies it, and next item took it place
- index--;
- }
- }
- // Return count of removed items
- return removeCounter;
- }
- });
- var arr = [1, 2, 3, 2, 2, 2];
- var itemsRemoved = arr.stackoverflow_remove(2);
- console.log(itemsRemoved);
- // 4
- console.log(arr);
- // [1, 3]
- var arr = ["tree", "bird", "car", "bird", "bird"];
- var itemsRemoved = arr.stackoverflow_remove("bird");
- console.log(itemsRemoved);
- // 3
- console.log(arr);
- // ["tree", "car"]
- /**
- * Removes all occurence of specified item from array
- * @param array Array
- * @param itemToRemove Item to remove from array
- * @returns {Number} Count of removed items
- */
- function stackoverflow_removeArrayItem(array, itemToRemove) {
- // Count of removed items
- var removeCounter = 0;
- // Iterate every array item
- for (var index = 0; index < array.length; index++) {
- // If current array item equals itemToRemove then
- if (array[index] === itemToRemove) {
- // Remove array item at current index
- array.splice(index, 1);
- // Increment count of removed items
- removeCounter++;
- // Decrement index to iterate current position
- // one more time, because we just removed item
- // that occupies it, and next item took it place
- index--;
- }
- }
- // Return count of removed items
- return removeCounter;
- }
- var arr = ["tree", "bird", "car", "bird", "bird"];
- var itemsRemoved = stackoverflow_removeArrayItem(arr, "bird");
- console.log(itemsRemoved);
- // 3
- console.log(arr);
- // ["tree", "car"]
- // Extending Array prototype with new function,
- // if that function is already defined in "Array.prototype",
- // then "Object.defineProperty" will throw an exception
- Object.defineProperty(Array.prototype, "stackoverflow_filterValue", {
- // Specify "enumerable" as "false" to prevent function enumeration
- enumerable: false,
- /**
- * Create new array where specified item is removed
- * @this Array
- * @param itemToRemove Item to remove from array
- * @returns {Number} Count of removed items
- */
- value: function (itemToRemove) {
- var filteredArray = this.filter(function(item){
- return item !== itemToRemove;
- });
- return filteredArray;
- }
- });
- var arr = [1, 2, 3, 2, 2, 2];
- // PAY ATTENTION.
- // Original array stay unchanged.
- var filteredArray = arr.stackoverflow_filterValue(2);
- console.log(filteredArray);
- // [1, 3]
- Array.prototype.remByVal = function(val) {
- for (var i = 0; i < this.length; i++) {
- if (this[i] === val) {
- this.splice(i, 1);
- i--;
- }
- }
- return this;
- }
- //Call like
- [1, 2, 3, 4].remByVal(3);
- array.splice( array.indexOf(item), 1 );
- // Array Remove - By John Resig (MIT Licensed)
- Array.prototype.remove = function(from, to) {
- var rest = this.slice((to || from) + 1 || this.length);
- this.length = from < 0 ? this.length + from : from;
- return this.push.apply(this, rest);
- };
- // Array Remove - By John Resig (MIT Licensed)
- Array.remove = function(array, from, to) {
- var rest = array.slice((to || from) + 1 || array.length);
- array.length = from < 0 ? array.length + from : from;
- return array.push.apply(array, rest);
- };
- Array.prototype.remove = function(from, to){
- this.splice(from, (to=[0,from||1,++to-from][arguments.length])<0?this.length+to:to);
- return this.length;
- };
- myArray.remove(8);
- function move(arr, val) {
- for (var i = 0, j = 0, l = arr.length; i < l; i++) {
- if (arr[i] !== val) {
- arr[j++] = arr[i];
- }
- }
- arr.length = j;
- }
- function indexof(arr, val) {
- var i;
- while ((i = arr.indexOf(val)) != -1) {
- arr.splice(i, 1);
- }
- }
- function splice(arr, val) {
- for (var i = arr.length; i--;) {
- if (arr[i] === val) {
- arr.splice(i, 1);
- }
- }
- }
- Remove all occurrences:
- move 0.0048 ms
- indexof 0.0463 ms
- splice 0.0359 ms
- Remove first occurrence:
- move_one 0.0041 ms
- indexof_one 0.0021 ms
- _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]
- var my_array = [1,2,3,4,5,6];
- delete my_array[4];
- console.log(my_array.filter(function(a){return typeof a !== 'undefined';}));
- remove_item = function (arr, value) {
- var b = '';
- for (b in arr) {
- if (arr[b] === value) {
- arr.splice(b, 1);
- break;
- }
- }
- return arr;
- }
- remove_item(array,value);
- function remove(arrOriginal, elementToRemove){
- return arrOriginal.filter(function(el){return el !== elementToRemove});
- }
- console.log( remove([1, 2, 1, 0, 3, 1, 4], 1) );
- function removeFromArray(array, item, index) {
- while((index = array.indexOf(item)) > -1) {
- array.splice(index, 1);
- }
- }
- //Set-up some dummy data
- var dummyObj = {name:"meow"};
- var dummyArray = [dummyObj, "item1", "item1", "item2"];
- //Remove the dummy data
- removeFromArray(dummyArray, dummyObj);
- removeFromArray(dummyArray, "item2");
- Array.prototype.destroy = function(obj){
- // Return null if no objects were found and removed
- var destroyed = null;
- for(var i = 0; i < this.length; i++){
- // Use while-loop to find adjacent equal objects
- while(this[i] === obj){
- // Remove this[i] and store it within destroyed
- destroyed = this.splice(i, 1)[0];
- }
- }
- return destroyed;
- }
- var x = [1, 2, 3, 3, true, false, undefined, false];
- x.destroy(3); // => 3
- x.destroy(false); // => false
- x; // => [1, 2, true, undefined]
- x.destroy(true); // => true
- x.destroy(undefined); // => undefined
- x; // => [1, 2]
- x.destroy(3); // => null
- x; // => [1, 2]
- A = [1, 2, 3, 4, 5, 6];
- A.splice($.inArray(3, A), 1);
- //It will return A=[1, 2, 4, 5, 6]`
- function removeAll(array, key){
- var index = array.indexOf(key);
- if(index === -1) return;
- array.splice(index, 1);
- removeAll(array,key);
- }
- Array.prototype.removeAll = function(key){
- var index = this.indexOf(key);
- if(index === -1) return;
- this.splice(index, 1);
- this.removeAll(key);
- }
- this.array = this.array.filter(function(element,i){
- return element.id !== idToRemove;
- });
- my_array.splice(idx, 1) for ele, idx in my_array when ele is this_value
- var index = jQuery.inArray(val,arr);
- if (index > -1) {
- arr.splice(index, 1);
- //console.log(arr);
- }
- var ArrayHelper = {
- remove: function(array, predict) {
- for (var i = 0; i < array.length; i++) {
- if (predict(array[i]) && i > -1) {
- return array.splice(i, 1);
- }
- }
- },
- removeAll: function(array, predict) {
- var removed = [];
- for (var i = 0; i < array.length; i++) {
- if (predict(array[i]) && i > -1) {
- removed.push(array.splice(i, 1));
- }
- }
- return removed;
- }
- };
- ArrayHelper.remove(myArray, function(row) { return row.id === 5 });
- ArrayHelper.removeAll(myArray, function(row) { return row.id > 3 && row.id < 15});
- var my_array = new Array();
- my_array.push("element1");
- var indexOf = function(needle)
- {
- if(typeof Array.prototype.indexOf === 'function') // newer browsers
- {
- indexOf = Array.prototype.indexOf;
- }
- else // older browsers
- {
- indexOf = function(needle)
- {
- var index = -1;
- for(var i = 0; i < this.length; i++)
- {
- if(this[i] === needle)
- {
- index = i;
- break;
- }
- }
- return index;
- };
- }
- return indexOf.call(this, needle);
- };
- var index = indexOf.call(my_array, "element1");
- my_array.splice(index, 1);
- Array.prototype.removeItem = function(a) {
- for (i = 0; i < this.length; i++) {
- if (this[i] == a) {
- for (i2 = i; i2 < this.length - 1; i2++) {
- this[i2] = this[i2 + 1];
- }
- this.length = this.length - 1
- return;
- }
- }
- }
- var recentMovies = ['Iron Man', 'Batman', 'Superman', 'Spiderman'];
- recentMovies.removeItem('Superman');
- array.key = null;
- function removeArrValue(arr,value) {
- var index = arr.indexOf(value);
- if (index > -1) {
- arr.splice(index, 1);
- }
- return arr;
- }
- function destroy(arr, val) {
- for (var i = 0; i < arr.length; i++) if (arr[i] === val) arr.splice(i, 1);
- return arr;
- }
- var myElement = "chocolate";
- var myArray = ['chocolate', 'poptart', 'poptart', 'poptart', 'chocolate', 'poptart', 'poptart', 'chocolate'];
- /* Important code */
- for (var i = myArray.length - 1; i >= 0; i--) {
- if (myArray[i] == myElement) myArray.splice(i, 1);
- }
- // Extending the core Array Object
- MyArray.prototype = new Array();
- MyArray.prototype.constructor= MyArray;
- /**
- * New array class constructor
- */
- function MyArray() {
- // Constructor code here
- }
- /**
- * Excludes a value from array and returns the rest of array
- * @param {string/number/boolean} excludedValue Value which should be removed
- * @return {array}
- */
- MyArray.prototype.without = function(excludedValue) {
- var valueType = typeof excludedValue;
- if (this.length < 1)
- return [];
- if (valueType == 'object' || valueType == 'array' || valueType == 'undefined')
- throw "Argument can not be object, array or undefined";
- for (var index in this) {
- if (this[index] === excludedValue) {
- this.splice(index, 1);
- index--;
- }
- };
- return this;
- };
- // How to use
- var arr = new MyArray();
- arr = [1,2,3,4,5,"name", false];
- arr.without(1); // will change the array to [2,3,4,5,"name", false]
- arr.without("name"); // will change the array to [2,3,4,5, false]
- arr.without(false); // will change the array to [2,3,4,5]
- arr.without([1,2]); // will throw error as argument can not be array
- arr.without({bar: "foo"}); // will throw error as argument can not be object
- $(document).ready(function(){
- var arr = ["C#","Ruby","PHP","C","C++"];
- var itemtoRemove = "PHP";
- arr.splice($.inArray(itemtoRemove, arr),1);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement