1

I have an array cornerId inside an object elementMatrix. Thereafter I want to create elemArray which is an array of the object elementMatrix. However I am not able to access the value of cornerId.

function elementMatrix() {
    var cornerId=new Array();
}

var elemArray=new Array();
elemArray[0]=new elementMatrix();
elemArray[0].cornerId[0]="z"; //if I put elemArray[0].cornerId="z"; then it works for the first element - but then how do I put second element???
elemArray[0].cornerId[1]="a";
alert(elemArray[0].cornerId[0]); // shows undefined
alert(elemArray[0].cornerId[1]); //again undefined
....
Add other elemArray values
....

I want to assign values and access values for the nth position of the array cornerId, which is a part of elemArray which is an array of object elementMatrix. Can anyone show me the right way to access cornerId values???

EDIT:

To clarify I want the liberty to add cornerId at the nth position (overwrite existing value) and not push it. Also I had not asked this originally but if there is a method to remove an nth position from cornerId then that will be great.

0

2 Answers 2

3

define your corner with this, push on arrays.... like this

function elementMatrix() {
    this.cornerId = [];
};
var elemArray = [];
var newMatrix =new elementMatrix();
newMatrix.cornerId.push('z');
newMatrix.cornerId.push('a');
elemArray.push(newMatrix);

alert(elemArray[0].cornerId[0]);
alert(elemArray[0].cornerId[1]);

or you can make your cornerid private and return public method like this (add is fluent)

function elementMatrix() {
    var cornerId = [];

    return {
        addCornerId: function(id) {
            cornerId.push(id);
            return this;
        },

        getCornerId: function(pos) {
            if(cornerId.length >= pos) {
                return cornerId[pos];
            }

            return null;
        }
    };
};
var elemArray = [];
var newMatrix =new elementMatrix();
newMatrix
    .addCornerId('z')
    .addCornerId('a');
elemArray.push(newMatrix);

alert(elemArray[0].getCornerId(0));
alert(elemArray[0].getCornerId(1));

edit, if you want a key value pair use a object instead a array like this

function elementMatrix() {
    var cornerId = {};
    var nextKey = 0;

    return {
        addCornerId: function(id, key) {
            if(!key && key !== 0) {
                key = nextKey;
            }
            cornerId[key] = id;
            nextKey++;
            return this;
        },

        removeCornerId: function(key) {
            if(cornerId[key]) {
                  delete cornerId[key]
            }

            return this;
        },
        getCornerId: function(key) {
            if(cornerId[key]) {
                return cornerId[key];
            }

            return null;
        }
    };
};
var elemArray = [];
var newMatrix =new elementMatrix();
newMatrix
    .addCornerId('z')
    .addCornerId('a')
    .addCornerId('XXX', 0)
    .addCornerId('YYYY', 'abc');
elemArray.push(newMatrix);

alert(elemArray[0].getCornerId(0));
alert(elemArray[0].getCornerId('abc'));
Sign up to request clarification or add additional context in comments.

Comments

2

First, don't use array initializer with new Array() use []; see Douglas Crockford's justification for that.

function elementMatrix() {
    var cornerId=[];
    return {
        cornerId: cornerId
    };
}
var elemArray=[];
elemArray.push(elementMatrix());
elemArray[0].cornerId.push("z");
elemArray[0].cornerId.push("a");
alert(elemArray[0].cornerId[0]);
alert(elemArray[0].cornerId[1]);

3 Comments

Thats a very quick and a good answer. Just one clarity is there a way to add the element in the nth position rather than push it at the end. Can i push the value at nth cornerId position of elemArray[0] (basically an overwrite). I have edited my question to be more clear.
Of course you can, but since you are changing a value in a specific position, it is not called pushing. myArray[index] = newvalue would do. take a look at w3schools.com
Oh it's embarrasing how simple it was. I was under the impression that I had tried this method and it didnt work. tx a ton

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.