Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.78 KB | None | 0 0
  1. var array = [2, 5, 9];
  2. var index = array.indexOf(5);
  3.  
  4. if (index > -1) {
  5. array.splice(index, 1);
  6. }
  7.  
  8. array.splice(i, 1);
  9.  
  10. for(var i = array.length - 1; i >= 0; i--) {
  11. if(array[i] === number) {
  12. array.splice(i, 1);
  13. }
  14. }
  15.  
  16. delete array[i];
  17.  
  18. delete array[ index ];
  19.  
  20. array.splice( index, 1 );
  21.  
  22. var value = array.splice( index, 1 )[0];
  23.  
  24. function remove(arr, item) {
  25. for(var i = arr.length; i--;) {
  26. if(arr[i] === item) {
  27. arr.splice(i, 1);
  28. }
  29. }
  30. }
  31.  
  32. // Extending Array prototype with new function,
  33. // if that function is already defined in "Array.prototype",
  34. // then "Object.defineProperty" will throw an exception
  35. Object.defineProperty(Array.prototype, "stackoverflow_remove", {
  36. // Specify "enumerable" as "false" to prevent function enumeration
  37. enumerable: false,
  38.  
  39. /**
  40. * Removes all occurence of specified item from array
  41. * @this Array
  42. * @param itemToRemove Item to remove from array
  43. * @returns {Number} Count of removed items
  44. */
  45. value: function (itemToRemove) {
  46. // Count of removed items
  47. var removeCounter = 0;
  48.  
  49. // Iterate every array item
  50. for (var index = 0; index < this.length; index++) {
  51. // If current array item equals itemToRemove then
  52. if (this[index] === itemToRemove) {
  53. // Remove array item at current index
  54. this.splice(index, 1);
  55.  
  56. // Increment count of removed items
  57. removeCounter++;
  58.  
  59. // Decrement index to iterate current position
  60. // one more time, because we just removed item
  61. // that occupies it, and next item took it place
  62. index--;
  63. }
  64. }
  65.  
  66. // Return count of removed items
  67. return removeCounter;
  68. }
  69. });
  70.  
  71. var arr = [1, 2, 3, 2, 2, 2];
  72.  
  73. var itemsRemoved = arr.stackoverflow_remove(2);
  74.  
  75. console.log(itemsRemoved);
  76. // 4
  77.  
  78. console.log(arr);
  79. // [1, 3]
  80.  
  81. var arr = ["tree", "bird", "car", "bird", "bird"];
  82.  
  83. var itemsRemoved = arr.stackoverflow_remove("bird");
  84.  
  85. console.log(itemsRemoved);
  86. // 3
  87.  
  88. console.log(arr);
  89. // ["tree", "car"]
  90.  
  91. /**
  92. * Removes all occurence of specified item from array
  93. * @param array Array
  94. * @param itemToRemove Item to remove from array
  95. * @returns {Number} Count of removed items
  96. */
  97. function stackoverflow_removeArrayItem(array, itemToRemove) {
  98. // Count of removed items
  99. var removeCounter = 0;
  100.  
  101. // Iterate every array item
  102. for (var index = 0; index < array.length; index++) {
  103. // If current array item equals itemToRemove then
  104. if (array[index] === itemToRemove) {
  105. // Remove array item at current index
  106. array.splice(index, 1);
  107.  
  108. // Increment count of removed items
  109. removeCounter++;
  110.  
  111. // Decrement index to iterate current position
  112. // one more time, because we just removed item
  113. // that occupies it, and next item took it place
  114. index--;
  115. }
  116. }
  117.  
  118. // Return count of removed items
  119. return removeCounter;
  120. }
  121.  
  122. var arr = ["tree", "bird", "car", "bird", "bird"];
  123.  
  124. var itemsRemoved = stackoverflow_removeArrayItem(arr, "bird");
  125.  
  126. console.log(itemsRemoved);
  127. // 3
  128.  
  129. console.log(arr);
  130. // ["tree", "car"]
  131.  
  132. // Extending Array prototype with new function,
  133. // if that function is already defined in "Array.prototype",
  134. // then "Object.defineProperty" will throw an exception
  135. Object.defineProperty(Array.prototype, "stackoverflow_filterValue", {
  136. // Specify "enumerable" as "false" to prevent function enumeration
  137. enumerable: false,
  138.  
  139. /**
  140. * Create new array where specified item is removed
  141. * @this Array
  142. * @param itemToRemove Item to remove from array
  143. * @returns {Number} Count of removed items
  144. */
  145. value: function (itemToRemove) {
  146. var filteredArray = this.filter(function(item){
  147. return item !== itemToRemove;
  148. });
  149.  
  150. return filteredArray;
  151. }
  152. });
  153.  
  154. var arr = [1, 2, 3, 2, 2, 2];
  155.  
  156. // PAY ATTENTION.
  157. // Original array stay unchanged.
  158. var filteredArray = arr.stackoverflow_filterValue(2);
  159.  
  160. console.log(filteredArray);
  161. // [1, 3]
  162.  
  163. Array.prototype.remByVal = function(val) {
  164. for (var i = 0; i < this.length; i++) {
  165. if (this[i] === val) {
  166. this.splice(i, 1);
  167. i--;
  168. }
  169. }
  170. return this;
  171. }
  172. //Call like
  173. [1, 2, 3, 4].remByVal(3);
  174.  
  175. array.splice( array.indexOf(item), 1 );
  176.  
  177. // Array Remove - By John Resig (MIT Licensed)
  178. Array.prototype.remove = function(from, to) {
  179. var rest = this.slice((to || from) + 1 || this.length);
  180. this.length = from < 0 ? this.length + from : from;
  181. return this.push.apply(this, rest);
  182. };
  183.  
  184. // Array Remove - By John Resig (MIT Licensed)
  185. Array.remove = function(array, from, to) {
  186. var rest = array.slice((to || from) + 1 || array.length);
  187. array.length = from < 0 ? array.length + from : from;
  188. return array.push.apply(array, rest);
  189. };
  190.  
  191. Array.prototype.remove = function(from, to){
  192. this.splice(from, (to=[0,from||1,++to-from][arguments.length])<0?this.length+to:to);
  193. return this.length;
  194. };
  195.  
  196. myArray.remove(8);
  197.  
  198. function move(arr, val) {
  199. for (var i = 0, j = 0, l = arr.length; i < l; i++) {
  200. if (arr[i] !== val) {
  201. arr[j++] = arr[i];
  202. }
  203. }
  204. arr.length = j;
  205. }
  206.  
  207. function indexof(arr, val) {
  208. var i;
  209. while ((i = arr.indexOf(val)) != -1) {
  210. arr.splice(i, 1);
  211. }
  212. }
  213.  
  214. function splice(arr, val) {
  215. for (var i = arr.length; i--;) {
  216. if (arr[i] === val) {
  217. arr.splice(i, 1);
  218. }
  219. }
  220. }
  221.  
  222. Remove all occurrences:
  223. move 0.0048 ms
  224. indexof 0.0463 ms
  225. splice 0.0359 ms
  226.  
  227. Remove first occurrence:
  228. move_one 0.0041 ms
  229. indexof_one 0.0021 ms
  230.  
  231. _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]
  232.  
  233. var my_array = [1,2,3,4,5,6];
  234. delete my_array[4];
  235. console.log(my_array.filter(function(a){return typeof a !== 'undefined';}));
  236.  
  237. remove_item = function (arr, value) {
  238. var b = '';
  239. for (b in arr) {
  240. if (arr[b] === value) {
  241. arr.splice(b, 1);
  242. break;
  243. }
  244. }
  245. return arr;
  246. }
  247.  
  248. remove_item(array,value);
  249.  
  250. function remove(arrOriginal, elementToRemove){
  251. return arrOriginal.filter(function(el){return el !== elementToRemove});
  252. }
  253. console.log( remove([1, 2, 1, 0, 3, 1, 4], 1) );
  254.  
  255. function removeFromArray(array, item, index) {
  256. while((index = array.indexOf(item)) > -1) {
  257. array.splice(index, 1);
  258. }
  259. }
  260.  
  261. //Set-up some dummy data
  262. var dummyObj = {name:"meow"};
  263. var dummyArray = [dummyObj, "item1", "item1", "item2"];
  264.  
  265. //Remove the dummy data
  266. removeFromArray(dummyArray, dummyObj);
  267. removeFromArray(dummyArray, "item2");
  268.  
  269. Array.prototype.destroy = function(obj){
  270. // Return null if no objects were found and removed
  271. var destroyed = null;
  272.  
  273. for(var i = 0; i < this.length; i++){
  274.  
  275. // Use while-loop to find adjacent equal objects
  276. while(this[i] === obj){
  277.  
  278. // Remove this[i] and store it within destroyed
  279. destroyed = this.splice(i, 1)[0];
  280. }
  281. }
  282.  
  283. return destroyed;
  284. }
  285.  
  286. var x = [1, 2, 3, 3, true, false, undefined, false];
  287.  
  288. x.destroy(3); // => 3
  289. x.destroy(false); // => false
  290. x; // => [1, 2, true, undefined]
  291.  
  292. x.destroy(true); // => true
  293. x.destroy(undefined); // => undefined
  294. x; // => [1, 2]
  295.  
  296. x.destroy(3); // => null
  297. x; // => [1, 2]
  298.  
  299. A = [1, 2, 3, 4, 5, 6];
  300. A.splice($.inArray(3, A), 1);
  301. //It will return A=[1, 2, 4, 5, 6]`
  302.  
  303. function removeAll(array, key){
  304. var index = array.indexOf(key);
  305.  
  306. if(index === -1) return;
  307.  
  308. array.splice(index, 1);
  309. removeAll(array,key);
  310. }
  311.  
  312. Array.prototype.removeAll = function(key){
  313. var index = this.indexOf(key);
  314.  
  315. if(index === -1) return;
  316.  
  317. this.splice(index, 1);
  318. this.removeAll(key);
  319. }
  320.  
  321. this.array = this.array.filter(function(element,i){
  322. return element.id !== idToRemove;
  323. });
  324.  
  325. my_array.splice(idx, 1) for ele, idx in my_array when ele is this_value
  326.  
  327. var index = jQuery.inArray(val,arr);
  328. if (index > -1) {
  329. arr.splice(index, 1);
  330. //console.log(arr);
  331. }
  332.  
  333. var ArrayHelper = {
  334. remove: function(array, predict) {
  335. for (var i = 0; i < array.length; i++) {
  336. if (predict(array[i]) && i > -1) {
  337. return array.splice(i, 1);
  338. }
  339. }
  340. },
  341. removeAll: function(array, predict) {
  342. var removed = [];
  343. for (var i = 0; i < array.length; i++) {
  344. if (predict(array[i]) && i > -1) {
  345. removed.push(array.splice(i, 1));
  346. }
  347. }
  348.  
  349. return removed;
  350. }
  351. };
  352.  
  353. ArrayHelper.remove(myArray, function(row) { return row.id === 5 });
  354. ArrayHelper.removeAll(myArray, function(row) { return row.id > 3 && row.id < 15});
  355.  
  356. var my_array = new Array();
  357.  
  358. my_array.push("element1");
  359.  
  360. var indexOf = function(needle)
  361. {
  362. if(typeof Array.prototype.indexOf === 'function') // newer browsers
  363. {
  364. indexOf = Array.prototype.indexOf;
  365. }
  366. else // older browsers
  367. {
  368. indexOf = function(needle)
  369. {
  370. var index = -1;
  371.  
  372. for(var i = 0; i < this.length; i++)
  373. {
  374. if(this[i] === needle)
  375. {
  376. index = i;
  377. break;
  378. }
  379. }
  380. return index;
  381. };
  382. }
  383.  
  384. return indexOf.call(this, needle);
  385. };
  386.  
  387. var index = indexOf.call(my_array, "element1");
  388.  
  389. my_array.splice(index, 1);
  390.  
  391. Array.prototype.removeItem = function(a) {
  392. for (i = 0; i < this.length; i++) {
  393. if (this[i] == a) {
  394. for (i2 = i; i2 < this.length - 1; i2++) {
  395. this[i2] = this[i2 + 1];
  396. }
  397. this.length = this.length - 1
  398. return;
  399. }
  400. }
  401. }
  402.  
  403. var recentMovies = ['Iron Man', 'Batman', 'Superman', 'Spiderman'];
  404. recentMovies.removeItem('Superman');
  405.  
  406. array.key = null;
  407.  
  408. function removeArrValue(arr,value) {
  409. var index = arr.indexOf(value);
  410. if (index > -1) {
  411. arr.splice(index, 1);
  412. }
  413. return arr;
  414. }
  415.  
  416. function destroy(arr, val) {
  417. for (var i = 0; i < arr.length; i++) if (arr[i] === val) arr.splice(i, 1);
  418. return arr;
  419. }
  420.  
  421. var myElement = "chocolate";
  422. var myArray = ['chocolate', 'poptart', 'poptart', 'poptart', 'chocolate', 'poptart', 'poptart', 'chocolate'];
  423.  
  424. /* Important code */
  425. for (var i = myArray.length - 1; i >= 0; i--) {
  426. if (myArray[i] == myElement) myArray.splice(i, 1);
  427. }
  428.  
  429. // Extending the core Array Object
  430. MyArray.prototype = new Array();
  431. MyArray.prototype.constructor= MyArray;
  432.  
  433. /**
  434. * New array class constructor
  435. */
  436. function MyArray() {
  437. // Constructor code here
  438. }
  439.  
  440. /**
  441. * Excludes a value from array and returns the rest of array
  442. * @param {string/number/boolean} excludedValue Value which should be removed
  443. * @return {array}
  444. */
  445. MyArray.prototype.without = function(excludedValue) {
  446.  
  447. var valueType = typeof excludedValue;
  448.  
  449. if (this.length < 1)
  450. return [];
  451.  
  452. if (valueType == 'object' || valueType == 'array' || valueType == 'undefined')
  453. throw "Argument can not be object, array or undefined";
  454.  
  455. for (var index in this) {
  456.  
  457. if (this[index] === excludedValue) {
  458.  
  459. this.splice(index, 1);
  460. index--;
  461.  
  462. }
  463. };
  464.  
  465. return this;
  466. };
  467.  
  468. // How to use
  469. var arr = new MyArray();
  470. arr = [1,2,3,4,5,"name", false];
  471.  
  472. arr.without(1); // will change the array to [2,3,4,5,"name", false]
  473. arr.without("name"); // will change the array to [2,3,4,5, false]
  474. arr.without(false); // will change the array to [2,3,4,5]
  475. arr.without([1,2]); // will throw error as argument can not be array
  476. arr.without({bar: "foo"}); // will throw error as argument can not be object
  477.  
  478. $(document).ready(function(){
  479. var arr = ["C#","Ruby","PHP","C","C++"];
  480. var itemtoRemove = "PHP";
  481. arr.splice($.inArray(itemtoRemove, arr),1);
  482. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement