tags:

views:

35

answers:

2

I have the code below. I want to send the value of value1

n.value1s = new Array();  
n.value1sIDs = new Array();  
n.value1sNames = new Array();  
n.value1sColors = new Array();  
n.descriptions = new Array();  

to

pg.loadLinkedvalue1s(n);   

and for value2 to pg.loadLinkedvalue2s(n);
Howd I do that in javascript without haveing to rewrite the complete function
please see the code below

    if(n.id == "row"){  
        n.rs = n.parentElement;  
        if(n.rs.multiSelect == 0){  
            n.selected = 1;  
            this.selectedRows = [ n ];  
            if(this.lastClicked && this.lastClicked != n){  
                selectionChanged = 1;  
                this.lastClicked.selected = 0;  
                this.lastClicked.style.color = "000099";  
                this.lastClicked.style.backgroundColor = "";  
            }  
        } else {  
            n.selected = n.selected ? 0 : 1;  
            this.getSelectedRows();  
        }  
        this.lastClicked = n;  


            n.value1s = new Array();  
            n.value1sIDs = new Array();  
            n.value1sNames = new Array();  
            n.value1sColors = new Array();  
            n.descriptions = new Array();  
            n.value2s = new Array();  
            n.value2IDs = new Array();  
            n.value2Names = new Array();  
            n.value2Colors = new Array();  
            n.value2SortOrders = new Array();  
            n.value2Descriptions = new Array();     
    var value1s = myOfficeFunction.DOMArray(n.all.value1s.all.value1);    
    var value2s = myOfficeFunction.DOMArray(n.all.value1s.all.value2);   

            for(var i=0,j=0,k=1;i<vaue1s.length;i++){  

                n.sortOrders[j] = k++;  
                n.vaue1s[j] = vaue1s[i].v;  
                n.vaue1IDs[j] = vaue1s[i].i;  
                n.vaue1Colors[j] = vaue1s[i].c;  
                alert(n.vaue1Colors[j]);  

    var vals = vaue1s[i].innerText.split(String.fromCharCode(127));       

                n.cptSortOrders[j] = k++;  
                n.value2s[j] = value2s[i].v;  
                n.value2IDs[j] = value2s[i].i;  
                n.value2Colors[j] = value2s[i].c;       
var value2Vals =  value2s[i].innerText.split(String.fromCharCode(127));    

                if(vals.length == 2){  
                    alert(n.vaue1Colors[j]);    
                    n.vaue1Names[j] = vals[0];  
                    n.descriptions[j++] = vals[1];  

                }   

                if(value2Vals.length == 2){  

            n.value2Names[j] = cptVals[0];    
                    alert(n.value2Names[j]);       
        n.cptDescriptions[j++] = cptVals[1];     
            alert(n.cptDescriptions[j++]);    
                }                 

            }


         //want to run this with value1 only
                pg.loadLinkedvalue1s(n);  

                // want to run this with value2 only  
                pg.loadLinkedvalue2s(n);      


    }  
+2  A: 
function makeValueObject() {
  return {
    values: [],
    ids: [],
    names: [],
    colors: [],
    // etc...
  }
}

n.value1 = makeValueObject();
n.value2 = makeValueObject();

n.value1.values.push('123');

function loadLinkedValues(valueObject) {
  // do stuff with valueObject //
}

loadLinkedValues(n.value1);

Also, I am assuming that that arrays go together. Meaning that values[i] is related to colors[i]. If this is the case, you can make this a lot more manageable.

function makeThing() {
  return {
    value: abc,
    id: 345,
    name: 'Cool Spiffy Thing',
    color: 'red'
  }
}

n.group1 = [];
n.group1.push(makeThing());
n.group1[0].name = 'Changed named here';

n.group1.push(makeThing());
n.group1.push(makeThing());

n.group2 = [makeThing()];

function loadLinkedValues(valueObjects) {
  for (var i=0; i < valueObjects.length; i++) {
    // do stuff with each object //
    console.log(valueObjects[i].name +'has a value of: '+ valueObjects[i].value);
  }
}

loadLinkedValues(n.group1);

So instead of having a single object with a ton on arrays, you have a single array with a ton of objects. And those objects simply have properties. The result is easier to conceptualize and maintain, and is overall cleaner.

Squeegy
+1 for more readable than the original.
wombleton
A: 

If I understand it right you're doing mass assigns of values to properties with the same suffix but different prefixes. You can create a function:

function massAssign(obj, prefix, value) {
  obj[prefix + 's'] = value;
  obj[prefix + 'sID1'] = value;
  obj[prefix + 'sRandom'] = value;
}

And then call it thus:

massAssign(n, 'value1', []);
wombleton