Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Object Data Structure Array Property

After an exhaustive search, I have yet to find a clear answer to address my problem. This may in part be due to incorrect or imprecise references to javascript terminology as object proiented programming is new to me.

var numArray = [0.123456789, 31.415, 314.15, -314.15, 0.0, 0, 1, 10, 100];

var AlignDecimal = function(NumberArray){

    var numberStructure = [{
        value:0,
        toString:"",
        decimalIndex:-1,
        integer:"",
        integerLength:-1,
        mantissa:"",
        mantissaLength:-1,
        sign:""
    }];

    for (var i = 0; i < NumberArray.length; i++){
        numberStructure[i].value = NumberArray[i];
        println(numberStructure[i].value);
    }

};

AlignDecimal(numArray);

I'm hoping from the above that there's enough information to ascertain my ultimate programming objective.

Originally I tried:

numberStructure.value = NumberArray

This partially worked except that .value had become an array, rather than numberStructure[].value.

Then I tried:

numberStructure = NumberArray

Now numberStructure[] array recieved NumberArray[] but, obviously, numberStructure[].value was undefined.

Also tried:

for (var i = 0; i < NumberArray.length; i++){
    numberStructure[i].value.push(NumberArray[i]);
    println(numberStructure[i].value);
}

That didn't work either.

like image 627
redthumb Avatar asked May 29 '26 10:05

redthumb


1 Answers

Are you trying to get an array of numberStructure objects, one for each number in numArray? If so, this is one way to do it:

var numArray = [0.123456789, 31.415, 314.15, -314.15, 0.0, 0, 1, 10, 100];

var AlignDecimal = function(NumberArray){

    function numberStructure(){
        this.value = 0;
        this.toString = "";
        this.decimalIndex = -1;
        this.integer = "";
        this.integerLength = -1;
        this.mantissa = "";
        this.mantissaLength = -1;
        this.sign = "";
    };

    var numberStructureArray = [];

    for (var i = 0; i < NumberArray.length; i++){
        var numStruct = new numberStructure();
        numStruct.value = NumberArray[i];
        numberStructureArray.push(numStruct);
        document.writeln(numberStructureArray[i].value + '<br/>');
    }

};

AlignDecimal(numArray);

You could also do it like this:

  
var numArray = [0.123456789, 31.415, 314.15, -314.15, 0.0, 0, 1, 10, 100];

var AlignDecimal = function(NumberArray){

    var numberStructureArray = [];

    for (var i = 0; i < NumberArray.length; i++){    
        numberStructureArray.push({
            value:NumberArray[i],
            toString:"",
            decimalIndex:-1,
            integer:"",
            integerLength:-1,
            mantissa:"",
            mantissaLength:-1,
            sign:""
        });
        document.writeln(numberStructureArray[i].value + '<br/>');
    }
};

AlignDecimal(numArray);
like image 124
adam0101 Avatar answered May 31 '26 22:05

adam0101



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!