在javascript中初始化自定义对象的数组

时间:2021-11-01 17:08:06

I have data coming in from the db I need to store in a js array of objects. How do I initialize this js array of objects , so that I can keep adding the data when I get it from the db .

我有数据来自我需要存储在js对象数组中的数据库。如何初始化此js对象数组,以便在从数据库中获取数据时可以继续添加数据。

The object structure will be of this format

对象结构将采用这种格式

var OsNames = [{deviceName:"TV",
releases: [
{release: "rel1", hws : [{hw:"h1"},{hw:"h2"},{hw:"h3"}]},
{release: "rel2", hws: [{hw:"h1"},{hw:"h2"},{hw:"h3"}]}
]},
{deviceName:"Phone",
  releases: [
  {release: "rel1", hws: [{hw:"h1"},{hw:"h2"},{hw:"h3"}]},
  {release: "rel2", hws: [{hw:"h1"},{hw:"h2"},{hw:"h3"}]}]
}];

2 个解决方案

#1


0  

I think that is what you are looking for.

我认为这就是你要找的东西。

var osNames = [];
// Get Data from Database
var myData = ajax.get(...);
for (var i=0; i < myData.length; i++) {
    osNames.push(myData[i]);
}

#2


0  

var OsNames = [];

You can add data to Array by pushing elements in it when you get it from the db

当您从数据库中获取数据时,可以通过推送数据向数组添加数据

e.g.

OsNames.push({release: "rel1", hws : [{hw:"h1"},{hw:"h2"},{hw:"h3"});

#1


0  

I think that is what you are looking for.

我认为这就是你要找的东西。

var osNames = [];
// Get Data from Database
var myData = ajax.get(...);
for (var i=0; i < myData.length; i++) {
    osNames.push(myData[i]);
}

#2


0  

var OsNames = [];

You can add data to Array by pushing elements in it when you get it from the db

当您从数据库中获取数据时,可以通过推送数据向数组添加数据

e.g.

OsNames.push({release: "rel1", hws : [{hw:"h1"},{hw:"h2"},{hw:"h3"});