Reorder javascript array data

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

LVL 44
scrathcyboyAsked:
Who is Participating?
 
scrathcyboyConnect With a Mentor Author Commented:
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
 
djon2003Commented:
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
 
scrathcyboyAuthor Commented:
please give the CODE for your idea.  Code is needed to get answer accepted.  Thanks
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
djon2003Commented:
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
 
scrathcyboyAuthor Commented:
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
 
djon2003Commented:
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
 
djon2003Commented:
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
 
djon2003Commented:
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
 
scrathcyboyAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.