1

I have a JSON object of the below format. Currently, this is hard coded. However, I would like to dynamically create this JSON.

I have another object which I need to loop through and then form this JSON object

[ { name: "test1", enable: true, unit: 24350, unit_price: 1.0368, composition: [ { asset: "asset1", percentage: 15 }, { asset: "asset2", percentage: 10 }, { asset: "asset3", percentage: 5 }, { asset: "asset4", percentage: 35 }, { asset: "asset5", percentage: 20 }, { asset: "asset6", percentage: 15 } ] }, { name: "test2", enable: true, unit: 24350, unit_price: 1.0368, composition: [ { asset: "asset1", percentage: 15 }, { asset: "asset2", percentage: 10 }, { asset: "asset3", percentage: 5 }, { asset: "asset4", percentage: 35 }, { asset: "asset5", percentage: 20 }, { asset: "asset6", percentage: 15 } ] }] 

Thanks in advance.

    2 Answers 2

    1

    function LoginController($scope) { $scope.post = [{ "name": "Shaw", "unit_price": 1586, "enable": true, "composition": [{ "asset": "asset1" }, { "percentage": 15 }] }, { "name": "Allen", "unit_price": 1586, "enable": false, "composition": [{ "asset": "asset2" }, { "percentage": 30 }] } ]; $scope.data = []; var tempObj1 = []; for (i = 0; i < $scope.post.length; i++) { var tempObj1 = { "name": $scope.post[i].name, "unit_price": $scope.post[i].unit_price, "enable": $scope.post[i].enable, }; tempObj1.composition = []; tempObj2 = { "asset": $scope.post[i].composition[0].asset, "percentage": $scope.post[i].composition[1].percentage }; tempObj1.composition.push(tempObj2); $scope.data.push(tempObj1); } console.log('data', $scope.data); }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script> <div ng-app ng-controller="LoginController"> <table> <tr> <th>Name</th> <th>Unit_Price</th> <th>Enable</th> <th>Compostion <table> <th>asset</th> <th>percentage</th> </table> </th> </tr> <tr ng-repeat="i in data"> <td>{{ i.name }}</td> <td>{{ i.unit_price }}</td> <td>{{ i.enable }}</td> <td>{{ i.composition[0].asset}}</td> <td>{{ i.composition[0].percentage }}</td> </tr> </table> </div>

      0

      Just use native Javascript JSON.parse

      var data = [ { name: "test1", enable: true, unit: 24350, unit_price: 1.0368, composition: [ { asset: "asset1", percentage: 15 }, { asset: "asset2", percentage: 10 }, { asset: "asset3", percentage: 5 }, { asset: "asset4", percentage: 35 }, { asset: "asset5", percentage: 20 }, { asset: "asset6", percentage: 15 } ] }, { name: "test2", enable: true, unit: 24350, unit_price: 1.0368, composition: [ { asset: "asset1", percentage: 15 }, { asset: "asset2", percentage: 10 }, { asset: "asset3", percentage: 5 }, { asset: "asset4", percentage: 35 }, { asset: "asset5", percentage: 20 }, { asset: "asset6", percentage: 15 } ] }] var json = JSON.parse(data); 
      2
      • Hi Andi, I want to create this object not parse it.
        – Frenz
        CommentedJul 2, 2015 at 3:18
      • What's the input? String or something?CommentedJul 2, 2015 at 4:04

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.