Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create nested json object in Angularjs

i want to create nested json object in angularjs. my object is this:

   {
    "marketerId": 1,
     "baskets": [
      {
        "customer": {
           "phone": ""
         },
        "region": 1,
        "orders": [
         {
           "bookId": 1,
           "count": 5
         },
         {
           "bookId": 2,
           "count": 52
         }
      ]
   },
    {
      "customer": {
          "phone": ""
     },
      "region": 1,
    "orders": [
     {
        "bookId": 1,
        "count": 12
     },
     {
        "bookId": 2,
        "count": 2
      }
     ]
   }
 ]
}

For create this object as dynamically i write this code.Assuming orders and items already have been initialized, the form is created. For example, the size of the items and orders 2.Is there a better way to build nested json objects?

     <input ng-model="formData.marketerId" />
    <div class="row" ng-repeat="item in items track by $index">
        <input ng-model="formData.baskets[$index].customer.phone" />
        <input ng-model="formData.baskets[$index].region" />
        <div  ng-repeat="order in orders track by $index">
           <input type="text" ng-model=
            "formData.baskets[$parent.$index].orders[$index].bookId">
             <input type="text" ng-model=
            "formData.baskets[$parent.$index].orders[$index].count">

        </div>
    </div>
like image 808
Hadi J Avatar asked Nov 09 '22 07:11

Hadi J


1 Answers

You can do something like this:

$scope.data1 = [];
var firstObj = new Object();
firstObj.first = "value1";
firstObj.second = "value2";
$scope.encountersData.push(firstObj);

$scope.data2 = [];
var  secondObj= new Object();
secondObj.third = "value3";
secondObj.fourth = "value4";
$scope.data2.push(secondObj);
like image 165
Nagarjuna Reddy Avatar answered Nov 14 '22 21:11

Nagarjuna Reddy