简体   繁体   English

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

[英]Initialize array of custom objects in javascript

I have data coming in from the db I need to store in a js array of objects.我有来自数据库的数据,我需要存储在一个 js 对象数组中。 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 对象数组,以便我可以在从 db 获取数据时继续添加数据。

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"}]}]
}];
var OsNames = [];

You can add data to Array by pushing elements in it when you get it from the db当您从数据库中获取数据时,您可以通过推送其中的元素来将数据添加到 Array

eg例如

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM