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.
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.
Share Improve this question edited Sep 18, 2015 at 12:33 pedromendessk 3,6562 gold badges27 silver badges36 bronze badges asked Dec 19, 2012 at 7:03 user1517108user1517108 2,4156 gold badges34 silver badges44 bronze badges 02 Answers
Reset to default 3define 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'));
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]);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744095370a4557845.html
评论列表(0条)