mohan raj
mohan raj

Reputation: 3

remove index with in the JSON using angular js

I have JSON value like below

        {
      "data": {
        "store": "54",
        "CountSheet": {
          "1": {        
            "totalInventory": 60,
            "totalInventoryCost": 360,
            "productCost": 20,
            "vendoreCode": "1001",
            "vendorName": "Sample 1"
          },
          "2": {    
            "totalInventory": 70,
            "totalInventoryCost": 360,
            "productCost": 30,
            "vendoreCode": "1002",
            "vendorName": "Sample 2"
          }
        }
      }
    }

I want to remove the index value "1" and "2" with in the Countsheet How to remove that, please help me to achieve this i want JSON like below

{
      "data": {
        "store": "54",
        "CountSheet": [
          {        
            "totalInventory": 60,
            "totalInventoryCost": 360,
            "productCost": 20,
            "vendoreCode": "1001",
            "vendorName": "Sample 1"
          },
          {    
            "totalInventory": 70,
            "totalInventoryCost": 360,
            "productCost": 30,
            "vendoreCode": "1002",
            "vendorName": "Sample 2"
          }
        ]
      }
    }

Upvotes: 0

Views: 276

Answers (2)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Since you have only two index 1 and 2 inside CountSheet index i have assumed only the presence of two so this code works really fine.

var jsonData =    {
      "data": {
        "store": "54",
        "CountSheet": {
          "1": {        
            "totalInventory": 60,
            "totalInventoryCost": 360,
            "productCost": 20,
            "vendoreCode": "1001",
            "vendorName": "Sample 1"
          },
          "2": {    
            "totalInventory": 70,
            "totalInventoryCost": 360,
            "productCost": 30,
            "vendoreCode": "1002",
            "vendorName": "Sample 2"
          }
        }
      }
    };
    
var obj1 = jsonData.data.CountSheet['1'];
var obj2= jsonData.data.CountSheet['2'];
jsonData.data.CountSheet = [];
jsonData.data.CountSheet.push(obj1);
jsonData.data.CountSheet.push(obj2);
console.log(jsonData);

Or if you have more than two index inside that object then use this code. It behaves dynamically

var jsonData =    {
      "data": {
        "store": "54",
        "CountSheet": {
          "1": {        
            "totalInventory": 60,
            "totalInventoryCost": 360,
            "productCost": 20,
            "vendoreCode": "1001",
            "vendorName": "Sample 1"
          },
          "2": {    
            "totalInventory": 70,
            "totalInventoryCost": 360,
            "productCost": 30,
            "vendoreCode": "1002",
            "vendorName": "Sample 2"
          }
        }
      }
    };
    
var tempArray = [];
var keys = Object.keys(jsonData.data.CountSheet);
for(var i=0; i<keys.length; i++){
  var key = keys[i];
  var obj = jsonData.data.CountSheet[key];
  tempArray.push(obj);
}
jsonData.data.CountSheet = tempArray;

console.log(jsonData);

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22564

var input = {
      "data": {
        "store": "54",
        "CountSheet": {
          "1": {        
            "totalInventory": 60,
            "totalInventoryCost": 360,
            "productCost": 20,
            "vendoreCode": "1001",
            "vendorName": "Sample 1"
          },
          "2": {    
            "totalInventory": 70,
            "totalInventoryCost": 360,
            "productCost": 30,
            "vendoreCode": "1002",
            "vendorName": "Sample 2"
          }
        }
      }
    }
var arr = Object.keys(input.data.CountSheet).map(function (key) { return input.data.CountSheet[key]; });
input.data.CountSheet = arr;
console.log(input);

Upvotes: 1

Related Questions