faqts : Computers : Programming : Languages : JavaScript : Database

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

19 of 27 people (70%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

JavaScript: Database: Simple: How to create one to one relation database using arrays in JavaScript?

Sep 11th, 2004 14:45
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 11 September 2004 - 11:21 pm ------------------

JavaScript: Database: Simple: How to create one to one relation 
database using arrays in JavaScript?

---

You could use an array with pointers to another array.

---

Steps: Overview:

 1. -Create 2 arrays

     1. The first array has minimally 2 columns,
        (one extra column [key] which contains a number referring to
         the corresponding row number of array 2)

     2. The second array has minimally 1 column

---
---

  array 1                            array 2

  --------------------               -------------------
  [description]  [key]               [nr]  [description]
  --------------------               -------------------
   A              4                   1     a

   B              2                   2     b

   C              1                   3     c

   D              3                   4     d

  ...

---
---

In JavaScript:

Steps: Worked out:

 1. -Create 2 arrays

     1. The first array has minimally 2 columns,
        (one extra column [key] which contains a number referring to
         the corresponding row number of array 2)

--- cut here: begin --------------------------------------------------

 var array1A = new Array (
  [ "A", "4" ],
  [ "B", "2" ],
  [ "C", "1" ],
  [ "D", "3" ]
 );

--- cut here: end ----------------------------------------------------

     2. The second array has minimally 1 column

--- cut here: begin --------------------------------------------------

 var array2A = new Array (
  [ "a" ],
  [ "b" ],
  [ "c" ],
  [ "d" ]
 );

--- cut here: end ----------------------------------------------------

 2. To view this arrays, you could use the following code:

--- cut here: begin --------------------------------------------------

<HTML>

<HEADER>

<SCRIPT LANGUAGE = "JavaScript">
<!--
<!-- library:   -->
 var array1A = new Array (
  [ "A", "4" ],
  [ "B", "2" ],
  [ "C", "1" ],
  [ "D", "3" ]
 );
// -->
</SCRIPT>

<SCRIPT LANGUAGE = "JavaScript">
<!--
<!-- library:   -->
 var array2A = new Array (
  [ "a" ],
  [ "b" ],
  [ "c" ],
  [ "d" ]
 );
// -->
</SCRIPT>

<SCRIPT LANGUAGE = "JavaScript">
<!--
<!-- library:   -->
function PROCDatabaseViewOneToOneArray( array1A, array2A ) {
 var I = 0;
 var minI = 0;
 var maxI = array1A.length - 1;
 var s1 = "";
 var keyI = 0;
 var s2 = "";
 // use a loop to go through each of the rows in array 1
 for ( I = minI; I <= maxI; I++ ) {
  s1 = array1A[ I ][ 0 ]; // get the description value of the first 
column of array 1 (at that row I)
  keyI = array1A[ I ][ 1 ] - 1; // get the key value of the second 
column of array 1 (at that row I)
  s2 = array2A[ keyI ]; // get the corresponding value of the first 
column of array 2 (using that key)
  document.write( s1 + " " + s2 );
  document.write( "<BR>" );
 }
}
// -->
</SCRIPT>

</HEADER>

<BODY
  ONLOAD = '
   PROCDatabaseViewOneToOneArray( array1A, array2A );
  '
>
</BODY>

</HTML>

--- cut here: end ----------------------------------------------------

 3. That will show the following output

--- cut here: begin --------------------------------------------------

  A d
  B b
  C a
  D c

--- cut here: end ----------------------------------------------------

---
---

Internet: see also:

---

Database: Data structure: Relation: Type: Main: Which are main 
database relation types? [one/many]
http://www.faqts.com/knowledge_base/view.phtml/aid/31339/fid/132

----------------------------------------------------------------------