如何以最有效的方式实现这个多维数组?

时间:2012-08-31 15:11:44

标签: javascript object multidimensional-array constructor object-literal

我想使用JavaScript制作这个数组:

    -----------------------------------------------
    |                     |         male          |
    |                     |-----------------------|
    |   rafael            |         single        |
    |                     |-----------------------|
    |                     |          22           | 
    |---------------------|-----------------------|
    |                     |         male          |
    |   jack              |-----------------------|
    |                     |         married       |      
    |                     |-----------------------|
    |                     |         34            |
    -----------------------------------------------

经过大量思考如何制作它之后我才使用它:

var arr = [];
for(i = 0 ; i < 2 ; i++){
    arr[i] = [2]
    for (j = 0; j < 3 ; j++){
        arr[i][1] = [3];
        arr[i][1][j] = "this is row :" + i + ", column : 1, row : " + j ;
        console.log(arr[i][1][j]);
    }
}

还有其他方法,使用对象构造函数或对象文字实现这一点吗? 因为我想像那样访问它

obj["first row"]["third row in second column"]

我希望用户在每个单元格中输入任何值,并按以下方式调用它:

obj["rafael"]["age"];

注意,我不知道有多少行,我只是放了任意值,以及任意的单元名称。

5 个答案:

答案 0 :(得分:2)

我不会把它变成一个数组,而是一个对象...因为你无论如何都像对象一样使用它。 (如果你没有使用整数索引和length属性,你就像使用基本对象一样使用数组。所有数组都是对象,并非所有对象都是数组。)

null替换为您正在使用的值,当然。或者您可以将它们留作占位符。

var a = {
  "first row": {
    "first row in second column": null,
    "second row in second column": null,
    "third row in second column": null
  },
  "second row": {
    "first row in second column": null,
    "second row in second column": null,
    "third row in second column": null
  }
}

啊,如果它是动态的,那么我会做这样的事情:

var folk = {};  // Our basic place holder.
var person = {};  // The 'inner information' to populate.
person.married = true;
person.age = 41;
person.address = "777 Heaven's Gate";
folk["Jeremy J Starcher"] = person;

// Next person
person = {};
person.gender = 'male';
person.insurance = true;
folk["John Doe"] = person;

window.alert(folk["John Doe"]["gender"]);
window.alert(folk["John Doe"].gender);

答案 1 :(得分:0)

如果要按字符串名称访问元素,则需要使用对象。数组仅支持数字索引。

var data = {
    'first row': {
        'first column': 'foo',
        'second column': 'bar',
        'third column': 'baz'
    },
    'second row': {
        'first column': 'foo',
        'second column': 'bar',
        'third column': 'baz'
    }
};

data['first row']['first column']; // gives you 'foo' 

请注意,只有在密钥名称包含无效的标识符字符(在本例中为空格)时才需要引用密钥名称

答案 2 :(得分:0)

这将为您提供具有动态属性的对象:

var arr = new Object();
for(i = 0 ; i < 2 ; i++){
    var rowName = "row"+i;
    arr[rowName] = new Object();
    for (j = 0; j < 3 ; j++){
        var colName = "col"+j;
        arr[rowName][colName] = "this is row " + i + ", column " + j;
    }
}
console.log(arr);

答案 3 :(得分:0)

我会将您的数据映射到包含以下内容的单元格:

    -----------------------------------------------
    | arr[0].left         | arr[0].right[0]       |
    |                     |-----------------------|
    |                     | arr[0].right[1]       |
    |                     |-----------------------|
    |                     | arr[0].right[2]       | 
    -----------------------------------------------
    | arr[1].left         | arr[1].right[0]       |
    |                     |-----------------------|
    |                     | arr[1].right[1]       |
    |                     |-----------------------|
    |                     | arr[1].right[2]       | 
    -----------------------------------------------

你可以这样做:

var arr = []

for(var i = 0; i < rows; i++) {
    arr[i] = {
        left: "someText",
        right: [
            "some",
            "more",
            "text"
        ]
    };
}

答案 4 :(得分:0)

看到您的修改,您要么:

var data = {
    rafael: {
        gender: "male",
        status: "single",
        age: "22"
    },
    jack: {
        gender: "male",
        status: "married",
        age: "34"
    }
};

var data = [{
    name: "rafael",
    gender: "male",
    status: "single",
    age: "22"
}, {
    name: "jack",
    gender: "male",
    status: "married",
    age: "34"
}];