Javascript:访问对象内的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13947145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:16:14  来源:igfitidea点击:

Javascript: Accessing array inside an object

javascriptarraysjavascript-objects

提问by user1517108

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

cornerId在对象中有一个数组elementMatrix。此后我想创建elemArray哪个是对象的数组elementMatrix。但是我无法访问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 elemArraywhich is an array of object elementMatrix. Can anyone show me the right way to access cornerIdvalues???

我想为数组的第 n 个位置分配值和访问值cornerId,它是elemArray对象数组的一部分elementMatrix。谁能告诉我访问cornerId值的正确方法???

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.

为了澄清我想要在第 n 个位置添加cornerId(覆盖现有值)而不是推送它的自由。此外,我最初没有问过这个问题,但是如果有一种方法可以从cornerId 中删除第 n 个位置,那就太好了。

回答by silly

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)

或者您可以将您的cornerid设为私有并返回这样的公共方法(添加流畅)

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'));

回答by Umur Kontac?

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

首先,不要将数组初始值设定项与new Array()use一起使用[];请参阅道格拉斯·克罗克福德 (Douglas Crockford) 对此的辩解。

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]);