Solved

Reorder javascript array data

Posted on 2008-06-19
9
1,605 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now