1

我想使用 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"];

请注意,我不知道会有多少行,我只是在其中放置任意值以及任意单元格名称。

4

5 回答 5

2

我不会让它成为一个数组,而是一个对象......因为无论如何你都像使用对象一样使用它。(如果您不使用整数索引和长度属性,则无论如何您都可以像使用基本对象一样使用数组。所有数组都是对象,并非所有对象都是数组。)

当然,替换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);
于 2012-08-31T15:16:43.963 回答
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' 

请注意,仅当键名具有无效的标识符字符(在本例中为空格)时,才需要引用键名

于 2012-08-31T15:19:21.353 回答
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);
于 2012-08-31T15:27:34.120 回答
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"
        ]
    };
}
于 2012-08-31T15:27:55.587 回答
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"
}];
于 2012-08-31T15:32:53.150 回答