7

There a simple function:

selected_row = []; // global scope

function toggleRowNumber(rowIndex) {
  if(selected_row[rowIndex]) selected_row.splice(rowIndex, 1);
  else selected_row[rowIndex] = 1; 
}

usage

toggleRowNumber(50000); // click the row - write the index
toggleRowNumber(50000); // click the row again - remove the inxed

alert(selected_row.length);

50001 OK

Delightful feature!

So is there a way to direct write|read an index without any searchin/looping? And without this huge feat as decribed above.

Thanks.

kostya
  • 209
  • 3
  • 11
  • Actually it [alerts](http://jsfiddle.net/33Ky6/1/) `50000` which is a correct value. What did you expect? – Teemu Jul 29 '13 at 06:19
  • Yes, 50000. I expect 0 after splice. It just like php does. $arr[50000] = 1; print sizeof($arr); // output 1 – kostya Jul 29 '13 at 08:41
  • Well, JS and PHP are different laguages. When `splice` shortens an array with length of 50001 by one, I'd expect the new length to be 50000 rather than 0... – Teemu Jul 29 '13 at 08:45

1 Answers1

0

If I understoold correctly, you want to store and index where you can check/set whether an item is selected or not. If that is the case, you are looking for a "key - value" data structure. Then, why not use a map?

var selected_row = {};

function toggleRowNumber(rowIndex) {
if(selected_row[rowIndex]) selected_row[rowIndex] = 0; //or = undefined;
else selected_row[rowIndex] = 1; 
}

That is better because hash map will save you time and space.

  • Space becuase you are not storing hundreds of 'undefined' values in a vector.
  • Time because, hash function used to access elements is pretended to hit the right position in many cases.
  • I think `undefined`s are not actually saved in an array, rather they are evaluated when reading the array. But time seems to be a keyword [here](http://jsperf.com/filled-array-vs-loose-array)... – Teemu Jul 29 '13 at 08:06
  • Thanks for reply. Should I kill elem at all to loop through only existentse as sush? – kostya Jul 29 '13 at 09:00
  • Imagine table with huge number of rows. Couple of rows selected. User clicked another. To deselect prevously selected ones I need loop all table to clear highlight class. But with selected_rows collection all I need is to check number of selected rows and toggle class just for them. So I found http://stackoverflow.com/questions/6295087/remove-item-from-object – kostya Jul 29 '13 at 09:12
  • 1
    Using a map (map = new Object() or map = {}; )the only think you have to do is to try to access to the element. Initially, for any key, its value is undefined. If you toggle one element on: map[rowIndex] = 1 – Pablo Francisco Pérez Hidalgo Jul 29 '13 at 09:34
  • Thanks, I have managed with _this_ but obj give me another silly question. In order to select a range of table rows I need to know the last (and probably the first) row added to selection. In array it's simply, the last: arr(arr.length-1) but objects goig around impossibilities sort them out. In practice it works as expected: last added item take last place in collection. But to get that last index I was forced to write a ridiculous code: for(var lastIndex in selected_row){}; since .length property does nod work. How it could be done in humanly way? – kostya Jul 29 '13 at 14:02
  • UPD. No, item collection doesnt work as expected. The last item always the biggest one. – kostya Jul 29 '13 at 14:39
  • Why don't you keep first and last toggled row index in two addtional global variables updated within "toggleRowNumber"? – Pablo Francisco Pérez Hidalgo Jul 29 '13 at 14:57
  • There is a 3-way selection table row: Click, ctrl+Click and Shift+Click. 1st deselect previous row and then select clicked new; 2nd (ctrl) add/remove row from selection and 3rd (shift) add range from the last selected row to the actual one. So I have to have a collection - not only first and last item. Finally I took the unused object variable and used it to store timestamp. BTW, may it will be useful for insert or delete db records as they were selected in time. – kostya Jul 29 '13 at 17:25