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.

3
  • Actually it alerts 50000 which is a correct value. What did you expect? Commented Jul 29, 2013 at 6:19
  • Yes, 50000. I expect 0 after splice. It just like php does. $arr[50000] = 1; print sizeof($arr); // output 1 Commented Jul 29, 2013 at 8: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... Commented Jul 29, 2013 at 8:45

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

8 Comments

I think undefineds are not actually saved in an array, rather they are evaluated when reading the array. But time seems to be a keyword here...
Thanks for reply. Should I kill elem at all to loop through only existentse as sush?
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 stackoverflow.com/questions/6295087/remove-item-from-object
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
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.