Solved

Reorder javascript array data

Posted on 2008-06-19
9
1,609 Views
Last Modified: 2012-08-14
I have a set of data I am reading from a cookie --
Name - number pairs

Currently it is ordered as (1) all the names first, then (2) all the numbers afterward.

I need it the opposite -- I need to follow each NAME with the NUMBER that relates to it.
This is likely as a NAME : VALUE pair.  The relevant code is below.

Can anyone arrange it to where each name is followed by its relevant number in the javascript array?

Also, as an aside, when I send this data via a form to PHP, the separator "|" in the code gets converted to a comma.
Any way to stop this?  Here is the relevant code to create the array and read it --

Please rearrange to where the value (i.e. number) follows the name it relates to in the array

var names = new Array();					// global array for cookie data
 
function setupForm(fRef, pg) {					// populate current page from cookie values, if present      
      tvpb_page = getCookie('tv-phbk' + pg);			
      if (tvpb_page) {						// if cookie not present, one blank page to fill in is OK
         names = tvpb_page.split('|');
       for (var e=0; e<fRef.elements.length; e++) {
       name = fRef.elements[e].name;
       switch (fRef.elements[e].type) {
       case 'text':
        if (names[e]) { 
        str = names[e];
        fRef.elements[e].value = str; 
        }
        break;
       }
    }
  }
}
 
function saveForm(fRef, Page) {				// save the current page as cookie with number
  var string = '';
  for (var e=0; e<fRef.elements.length; e++) {
    name = fRef.elements[e].name;
    switch (fRef.elements[e].type) {
      case 'text':
        if (string != '') string += '|';
        string += fRef.elements[e].value;
        break;
       }
    }

Open in new window

0
Comment
Question by:scrathcyboy
  • 5
  • 4
9 Comments
 
LVL 9

Expert Comment

by:djon2003
ID: 21827641
What I would suggest is to build an array where each line is set this way :
NAME§NUMBER

After then, using the sort method (http://www.w3schools.com/jsref/jsref_sort.asp) of the array will allow you to have this sorted by name. Using a split method or and lastIndexOf method and substring method to get the number.
0
 
LVL 44

Author Comment

by:scrathcyboy
ID: 21827905
please give the CODE for your idea.  Code is needed to get answer accepted.  Thanks
0
 
LVL 9

Expert Comment

by:djon2003
ID: 21828050
Could you clarify some points ..
What did you mean by :
Currently it is ordered as (1) all the names first, then (2) all the numbers afterward ? What are names ? and numbers ? key & value ? If so, the way it seems to be stored do not reflect this. It seems to me that only values are saved. Depending on what names are..

Thanks to rephrase, maybe I'm not understanding something right.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 44

Author Comment

by:scrathcyboy
ID: 21828159
name1, name2, name3, name4 ....., number1, number2,number3, number 4 .....
I need it
name1, number1, name2, number2, name3, number 3 name4, number 4 ......
Is this not clear?

"maybe I'm not understanding something right."
evidently
0
 
LVL 9

Expert Comment

by:djon2003
ID: 21828221
From what I see of what you have.. the saveForm function should be modified to store this way :

  for (var e=0; e<fRef.elements.length/2; e++) {
    name = fRef.elements[e].name;
    switch (fRef.elements[e].type) {
      case 'text':
        if (string != '') string += '|';
        string += fRef.elements[e].value + '|' + fRef.elements[e+fRef.elements.length/2];
        break;
       }
    }

Open in new window

0
 
LVL 9

Expert Comment

by:djon2003
ID: 21828226
You have to adjust the load function too..

Corrected :
 for (var e=0; e<fRef.elements.length/2; e++) {
    name = fRef.elements[e].name;
    switch (fRef.elements[e].type) {
      case 'text':
        if (string != '') string += '|';
        string += fRef.elements[e].value + '|' + fRef.elements[e+fRef.elements.length/2]value;
        break;
       }
    }

Open in new window

0
 
LVL 9

Expert Comment

by:djon2003
ID: 21828229
for (var e=0; e<fRef.elements.length/2; e++) {
    name = fRef.elements[e].name;
    switch (fRef.elements[e].type) {
      case 'text':
        if (string != '') string += '|';
        string += fRef.elements[e].value + '|' + fRef.elements[e+fRef.elements.length/2].value;
        break;
       }
    }
0
 
LVL 44

Author Comment

by:scrathcyboy
ID: 21836253
your code doesn't work, and I lose all my cookie data.  I just want to reorder the way it appears in the cookie, NOT lose the whole lot !!!
0
 
LVL 44

Accepted Solution

by:
scrathcyboy earned 0 total points
ID: 21903572
I found the solution to this, it is to use standard array indexing, like --

var first_name = names[1];
var first_value = names[21];

and so forth.  Since the array is all names first, then values, then the offset of 20 gets the value for each name.  So simple.  I was fooled by this name=value pair nonsense for cookie arrays, which I think is a big boondoggle that forces one to search the array repeatedly for names, just to find their values.

This is extremely inefficient, and wastes time and intellect.  The direct index method is lightning fast and intuitive.

I STRONGLY RECOMMEND to anyone handling cookies -- make your cookie values and data the way you want for fast efficient cookie handling.  Don't follow this NAME=VALUE paired nonsense that you read in 99.99% of articles discussing cookie handling.  You can make and handle a cookie any way you want.

So do it the logical way, with direct indexing of the array.  This is the solution to this question.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question