?
Solved

Reorder javascript array data

Posted on 2008-06-19
9
Medium Priority
?
1,614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

770 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