Solved

Reorder javascript array data

Posted on 2008-06-19
9
1,613 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

726 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