0

I want to iterate through following json and create a dynamic form,. But, both its key and value are not same. So how do I iterate it in JS?

["register_form",
   {  
      "name":{  
         "fielde":"textfield",
         "description":""
      },
      "pass":{  
         "pass1":{  
            "field":"password"
         },
         "pass2":{  
            "field_type":"password"
         }
      },
      "mail":{  
         "field":"textfield",
         "description":""
      },
      "field_first_name":{  
         "field_type":"textfield",
         "description":""
      }
      
   }
]

none of these working:

//Get the size of object = no. of fileds to be created
var size = Object.keys(response[1]).length; 
console.log("Size..." + size);

console.log("Keys.." + Object.keys(response[1]));
var keys = [];
var model = [];
var label = [];
keys.push(Object.keys(response[1]));

for(var i=0;i<keys.length;i++) {
   angular.forEach(response[1].keys[i], function(key, value){
   console.log(key + "..." + value);            
});
          

4
  • Have you tried for..in loop to iterate through object? Commented May 18, 2016 at 9:15
  • I did it, but unable to fetch both key and its value. e,g,: I want to fetch "name" and its value - another obj with field and description as keys and resp values of it Commented May 18, 2016 at 9:25
  • how many fields need to be created, 5? Your data keys are not consistent.. You have ( field, fielde, field_type ). Are they typos? You also have nested objects inside "pass", is that intentional as well? Commented May 18, 2016 at 12:44
  • yes, its intentional Commented May 19, 2016 at 8:17

3 Answers 3

1

You can do it with Vanilla javascript like this:

for (key in response[1]){
console.log(key)
};

JSFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

don't forget about .hasOwnProperty() check
1

Try This

var jimApp = angular.module("mainApp",  []);

jimApp.controller('mainCtrl', function($scope, $sce){
$scope.user = {};
  $scope.register_form = {"name":{  
                         "field_type":"text",
                         "description":""
                      },
                      "pass1":{ 
                            "field_type":"password"
                       },
                       "pass2":{ 
                            "field_type":"password"
                       },
                      "mail":{  
                         "field_type":"text",
                         "description":""
                      },
                      "field_first_name":{  
                         "field_type":"text",
                         "description":""
                      } };
  $scope.formData = { name : "" };
  $scope.html = "";
  angular.forEach($scope.register_form, function(value, key){
    $scope.html = $scope.html + "<input type="+ value.field_type +" placeholder="+key+" >";
   });
  $scope.trustedHtml = $sce.trustAsHtml($scope.html);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainCtrl">
  <form>
    <div ng-bind-html="trustedHtml">
    </div>
  </form>  
</div>

3 Comments

I want to iterate in controller not html/tempalte
What is the need to iterate in the controller?
i wnat to create a dynamic form, for which i am using - github.com/danhunsaker/angular-dynamic-forms
0

Updated

I think this is what you need. You can format the output whatever way you want.

var data = {  
      "name":{  
         "fielde":"textfield",
         "description":""
      },
      "pass":{  
         "pass1":{  
            "field":"password"
         },
         "pass2":{  
            "field_type":"password"
         }
      },
      "mail":{  
         "field":"textfield",
         "description":""
      },
      "field_first_name":{  
         "field_type":"textfield",
         "description":""
      }
   };
function toArray(obj) {
    var result = [];var passName;var tempArray = [];
    for (var prop in obj) {
        var value = obj[prop];
        if (typeof value === 'object') {
                $('#myForm').append("<br/><b>[[" + prop +"]]</b><br/>");

            if ($.isNumeric(prop)) {
                passName = name + "[" + prop + "]";
            } else {
                passName = name + "['" + prop + "']";
            }
            tempArray = toArray(value, passName);
            $.each(tempArray, function (key, value) {
                result.push(value);
            });
        } else {
        $('#myForm').append("<label>" + prop +"</label> : <span>" + obj[prop] + "</span><br/>");
            result.push(name + "['" + prop + "']");
        }
    }
    return result;
}

toArray(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myForm">

</div>

1 Comment

var arr = Object.keys(response).map(function(k) { response[k] }); tried this too, no much help!

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.