0

Hi I am developing web application in angularjs. I have one html form with several drop downs. I am making api calls to fill data to drop down. Instead of making separate call in one call i am getting all data required to bind all drop downs. Below is my sample data.

{"status":"Success","msg":"Success","code":"200","data":{"Education":[{"ID":1,"Education":"btech"},{"ID":2,"Education":"Bcom"},{"ID":3,"Education":"BA"},{"ID":6,"Education":"PU"}],"MaritalStatus":[{"ID":1,"MaritalStatus":"sinle"},{"ID":2,"MaritalStatus":"married"}]} 

I am trying to bind it to drop down as below.

var martialstatus = new Array();
    var Education = new Array();
    $http.get(baseurl + 'api' + '/Customer/' + 'PersonalInfoMasters').success(function (data) {
        $.map(data.data, function (item) {
            Education.push(item[0]);
        });
        console.log(Education);
    }).error(function (status) {
    });

Abobe piece of code gets me first item from the each object like first item from education,martial status etc. I would like to bind education object to education array. May i know where i am missing any line of code? Any help would be appreciated.

1
  • $.map(data.data.Education Use this. Commented May 26, 2017 at 9:16

4 Answers 4

2

You don't seem to do any kind of mapping from the original result, so the following would suffice:

Education = data.data.Education;

Note:

  1. The item[0] looks strange - are you trying to achieve something with that ?
  2. I would use lowercase for the name of the variable.
Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this:

var MaritalStatus = new Array();
var Education = new Array();

$http.get(baseurl + 'api' + '/Customer/' + 'PersonalInfoMasters').success(function (data) {

    $.map(data.data.Education, function (item) {
        Education.push(item);
    });

    $.map(data.data.MaritalStatus, function (item) {
        MaritalStatus.push(item);
    });

}).error(function (error) {

    // handle API errors

});

If you do not even need to format the output, you can simply re-assign the original value of your variable.

$http.get(baseurl + 'api' + '/Customer/' + 'PersonalInfoMasters').success(function (data) {

    Education = data.data.Education;
    MaritalStatus = data.data.MaritalStatus;

}).error(function (error) {

    // handle API errors

});

Comments

1

You can do it this way:

var martialstatus = new Array();
var Education = [];
$http.get(baseurl + 'api' + '/Customer/' + 'PersonalInfoMasters').success(function(data) {
  $.map(data.data.Education, function(item) {
      Education.push(item);
  });

}).error(function(status) {
});

You just need to push item instead of item[0] in the .map() method so it takes the whole Education object and push it in your array.

Otherwise you can just use with no need to use the $.map():

Education = data.data.Education

Comments

0

Shouldn't it be more like:

$.map(data.data.Education, function (item) {
        Education.push(item[0]);
    });

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.