在Javascript中创建一个对象数组

时间:2022-03-14 16:54:23

I want to create this structure dynamically in Javascript:

我想在Javascript中动态创建此结构:

var myCols = [
  {data1: 'A', data2: '0', data3: 1},
  {data1: 'B', data2: '1', data3: 1},
  {data1: 'C', data2: '2', data3: 1}
];

How can I do this?

我怎样才能做到这一点?

Thank you in Advance.

先感谢您。

3 个解决方案

#1


1  

Your question is very vague. There are infinitely many ways to generate that dynamically. How you do it depends on the data you start with and how and when you want to create it. For example you could generate exactly that above code dynamically on the server side after a DB query, or you could generate it with something like this loop, which would go own through more letters in data1 and more numbers (as strings) in data2 if you increase 3 to something less than or equal to 26.

你的问题很模糊。动态生成动态的方法有很多种。如何操作取决于您开始使用的数据以及创建它的方式和时间。例如,您可以在数据库查询后在服务器端动态生成上述代码,或者您可以使用类似此循环的内容生成它,这将通过data1中的更多字母和data2中的更多数字(作为字符串)来拥有增加3到小于或等于26的东西。

var myCols = [];
var myData, i;
for(i = 0; i < 3; i++){
    myData = {};
    myData.data1 = String.fromCharCode(65 + i);
    myData.data2 = "" + i;
    myData.data3 = 1;

    myCols.push(myData);
}

#2


0  

Is this what you mean? Use push to add items "dynamically" to an array.

你是这个意思吗?使用push将项“动态”添加到数组中。

// Create empty array
var myCols = [];

// Add items at a later time to the array using push
myCols.push( {data1: 'A', data2: '0', data3: 1});
// ... (repeat pushing more items)

#3


0  

Use push method of array object you can use it like

使用数组对象的push方法就可以了

var anarr = [] or var anarr = new Array();

var anarr = []或var anarr = new Array();

and then you can push elements to array one by one using

然后你可以使用逐个元素推送到数组

anarr.push(value);

where value can be anything.

价值可以是任何东西。

Try to understand following code.

尝试了解以下代码。

var keys = ['data1','data2','data3'];
var arr = new Array();

    function add(a,b,c){
        temp_dict = {}
        temp_dict[keys[0]]=a
        temp_dict[keys[1]]=b
        temp_dict[keys[2]]=c
        arr.push(temp_dict);
    }
        add('A','0',1);
        add('B','1',1);
        add('C','2',1);
        console.log(arr);

#1


1  

Your question is very vague. There are infinitely many ways to generate that dynamically. How you do it depends on the data you start with and how and when you want to create it. For example you could generate exactly that above code dynamically on the server side after a DB query, or you could generate it with something like this loop, which would go own through more letters in data1 and more numbers (as strings) in data2 if you increase 3 to something less than or equal to 26.

你的问题很模糊。动态生成动态的方法有很多种。如何操作取决于您开始使用的数据以及创建它的方式和时间。例如,您可以在数据库查询后在服务器端动态生成上述代码,或者您可以使用类似此循环的内容生成它,这将通过data1中的更多字母和data2中的更多数字(作为字符串)来拥有增加3到小于或等于26的东西。

var myCols = [];
var myData, i;
for(i = 0; i < 3; i++){
    myData = {};
    myData.data1 = String.fromCharCode(65 + i);
    myData.data2 = "" + i;
    myData.data3 = 1;

    myCols.push(myData);
}

#2


0  

Is this what you mean? Use push to add items "dynamically" to an array.

你是这个意思吗?使用push将项“动态”添加到数组中。

// Create empty array
var myCols = [];

// Add items at a later time to the array using push
myCols.push( {data1: 'A', data2: '0', data3: 1});
// ... (repeat pushing more items)

#3


0  

Use push method of array object you can use it like

使用数组对象的push方法就可以了

var anarr = [] or var anarr = new Array();

var anarr = []或var anarr = new Array();

and then you can push elements to array one by one using

然后你可以使用逐个元素推送到数组

anarr.push(value);

where value can be anything.

价值可以是任何东西。

Try to understand following code.

尝试了解以下代码。

var keys = ['data1','data2','data3'];
var arr = new Array();

    function add(a,b,c){
        temp_dict = {}
        temp_dict[keys[0]]=a
        temp_dict[keys[1]]=b
        temp_dict[keys[2]]=c
        arr.push(temp_dict);
    }
        add('A','0',1);
        add('B','1',1);
        add('C','2',1);
        console.log(arr);