Adarsh M Pallickal Adarsh M Pallickal - 5 days ago 7
AngularJS Question

Array format error

I have an array as following

{"contractID":27112016185818,"contractName":"ddf","supplierID":5,"supplierName":"hh","productID":44,"productName":"gg","uom":"uu","quantity":6,"pricePerUOM":6,"totalPrice":36,"currency":"inr","supplyByDate":"01112016","createdDate":"27112016","loginuser":"buyer","chain_id":"supplychainintegrity","height":244432,"num_txs":1,"pendingWith":"","block_hash":"hash","block_data":"data","data_hash":"datahash","block_time":"time"}

I want to convert it into the following

[{"contractID":27112016185818,"contractName":"ddf","supplierID":5,"supplierName":"hh","productID":44,"productName":"gg","uom":"uu","quantity":6,"pricePerUOM":6,"totalPrice":36,"currency":"inr","supplyByDate":"01112016","createdDate":"27112016","loginuser":"buyer","chain_id":"supplychainintegrity","height":244432,"num_txs":1,"pendingWith":"","block_hash":"hash","block_data":"data","data_hash":"datahash","block_time":"time"}]


Its for json stringification.

jsonObj["row"]=currentArray;
newArray=JSON.stringify(jsonObj);


My newArray as follows

{"row":{"contractID":27112016185818,"contractName":"ddf","supplierID":5,"supplierName":"hh","productID":44,"productName":"gg","uom":"uu","quantity":6,"pricePerUOM":6,"totalPrice":36,"currency":"inr","supplyByDate":"01112016","createdDate":"27112016","loginuser":"buyer","chain_id":"supplychainintegrity","height":244432,"num_txs":1,"pendingWith":"","block_hash":"hash","block_data":"data","data_hash":"datahash","block_time":"time"}}


need as following

{"row":[{"contractID":27112016185818,"contractName":"ddf","supplierID":5,"supplierName":"hh","productID":44,"productName":"gg","uom":"uu","quantity":6,"pricePerUOM":6,"totalPrice":36,"currency":"inr","supplyByDate":"01112016","createdDate":"27112016","loginuser":"buyer","chain_id":"supplychainintegrity","height":244432,"num_txs":1,"pendingWith":"","block_hash":"hash","block_data":"data","data_hash":"datahash","block_time":"time"}]}

Answer

You can simply do this,

 $scope.newarray = [];
 $scope.myObj = data.row;
 $scope.newarray.push($scope.myObj);
 console.log($scope.newarray);

DEMO

Comments