简体   繁体   English

将Javascript对象转换为Javascript数组

[英]Converting Javascript object to Javascript array

I am using a knockout observable array to populate a dropdownlist.The dropdownlist gets populated if I hardcode the array values.But I am now trying to get the data from the database.I am using JSON format for the javascript object.I am accessing a single property of the javascript object ie; 我正在使用一个可剔除的可观察数组来填充一个dropdownlist。如果我对数组值进行硬编码,则会填充dropdownlist。但是我现在正尝试从数据库中获取数据。我正在使用JSON格式的javascript对象。 javascript对象的单个属性,即; Certification and I am creating an array which I am passing back to a script.But each time the value that is passed back is undefined whereas the array shows up in the console. 认证,我正在创建一个要传递回脚本的数组,但是每次传递回的值都是不确定的,而该数组会显示在控制台中。

The code to convert to an array that I am using is 转换为我正在使用的数组的代码是

var getCertifications = function () {
    $.ajax({
    type : "GET",
    async : false,
    url : "/Provider/GetCertifications",
    dataType : "json",
    success: function (data) {
        var arrCertification = [];
        $.each(data, function (i, item) {
            arrCertification.push((item.Certification));
        });
        return arrCertification;
    },
    error : function () {
        alert(" An error occurred.");
        }
     });
 }; 

The code for the knockout observables is 淘汰赛可观察到的代码是

  var certificates = getCertifications();
  self.certificationArray = ko.observableArray(certificates);

and the HTML is HTML是

 <div class="form-group">
            <label class="col-sm-2 control-label labelfont">Certification:</label>
            <div class="col-sm-6">
                <select class="form-control" id="certification" name="certification" data-bind="value: certification, options: certificationArray, optionsCaption: 'Select a Certification'">
                </select>
            </div>
        </div>

Could some one please guide me on where I am going wrong. 有人可以指导我哪里出错了。

The getCertifications() function doesn't return anything. getCertifications()函数不返回任何内容。 All it does is kick off an ajax call. 它所做的只是启动一个ajax调用。

You need to use some sort of call back or promise object to get the results of this call. 您需要使用某种回调或promise对象来获取此调用的结果。 Using a callback this would look something like: 使用回调看起来像:

var getCertifications = function (cb) {
    $.ajax({
    type : "GET",
    async : false,
    url : "/Provider/GetCertifications",
    dataType : "json",
    success: function (data) {
        var arrCertification = [];
        $.each(data, function (i, item) {
            arrCertification.push((item.Certification));
        });
        cb(arrCertification);
    },
    error : function () {
        alert(" An error occurred.");
        }
     });
 };

 getCertifications(function(certificates) {
     self.certificationArray = ko.observableArray(certificates);
 });

A better pattern would be to use promises. 更好的模式是使用诺言。 The $.ajax method returns a promise. $ .ajax方法返回一个promise。 However the callback approach is a good place to start in order to get an understanding of handling asynchronous code. 但是,为了了解异步代码的处理方法,回调方法是一个不错的起点。

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

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