Create One dimensional array JSON JavaScript selected Key

Here you will see how to create One dimensional array JSON JavaScript selected Key by using for loop.

  • Supplied JSON object:
[
	{
		"ric": "ADAG.DE",
		"sedol": "5653487",
		"quantity": 107479,
		"activityQuantity": null,
		"businessDate": 20141023
	},
	{
		"ric": "ADAG.DE",
		"sedol": "BBJPFY1",
		"quantity": 2758,
		"activityQuantity": null,
		"businessDate": 20141023
	},

	{
		"ric": "ADAG.DE",
		"sedol": "5653487",
		"quantity": 107479,
		"activityQuantity": null,
		"businessDate": 20141023
	},

	{
		"ric": "BDAG.DE",
		"sedol": "5653487",
		"quantity": 107479,
		"activityQuantity": null,
		"businessDate": 20141023
	},

	{
		"ric": "BDAG.DE",
		"sedol": "5653487",
		"quantity": 107479,
		"activityQuantity": null,
		"businessDate": 20141023
	}
]
  • Final out put JSON object based on user selection of “ric”: “ADAG.DE” for the data above as below:
[
	{
		"key": "ADAG.DE",
		"values": [
			{
				"ric": "ADAG.DE",
				"sedol": "5653487",
				"quantity": 107479,
				"activityQuantity": null,
				"businessDate": 20141023
			},
			{
				"ric": "ADAG.DE",
				"sedol": "BBJPFY1",
				"quantity": 2758,
				"activityQuantity": null,
				"businessDate": 20141023
			},
			{
				"ric": "ADAG.DE",
				"sedol": "5653487",
				"quantity": 107479,
				"activityQuantity": null,
				"businessDate": 20141023
			}
		]
	}
]
  • HTML code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Create two dimensional array JSON JavaScript selected Key</title>
<script>

    function buildJSONData(){
       
    	var data = [
 	        	{
 	        	    "ric": "ADAG.DE",
 	        	    "sedol": "5653487",
 	        	    "quantity": 107479,
 	        	    "activityQuantity": null,
 	        	    "businessDate": 20141023
 	        	},
 	        	{
 	        	    "ric": "ADAG.DE",
 	        	    "sedol": "BBJPFY1",
 	        	    "quantity": 2758,
 	        	    "activityQuantity": null,
 	        	    "businessDate": 20141023
 	        	},

 	        	{
 	        	    "ric": "ADAG.DE",
 	        	    "sedol": "5653487",
 	        	    "quantity": 107479,
 	        	    "activityQuantity": null,
 	        	    "businessDate": 20141023
 	        	},

 	        	{
 	        	    "ric": "BDAG.DE",
 	        	    "sedol": "5653487",
 	        	    "quantity": 107479,
 	        	    "activityQuantity": null,
 	        	    "businessDate": 20141023
 	        	},

 	        	{
 	        	    "ric": "BDAG.DE",
 	        	    "sedol": "5653487",
 	        	    "quantity": 107479,
 	        	    "activityQuantity": null,
 	        	    "businessDate": 20141023
 	        	}
	        	];

    	var tempmap = {};
    	var jsonData = [];
    	var selectedRic = "ADAG.DE";

    	for ( var j = 0; j < data.length; j ++ )
    	{
			if (data[j].ric == selectedRic) {
				
				if (!(data[j].ric in tempmap))
	    		{
	    			jsonData.push (
	    			{
	    				key: data[j].ric,
	    				values: []
	    			}
	    			);
	    			tempmap[data[j].ric] = jsonData.length - 1;
	    		}
	    		
	    		jsonData[tempmap[data[j].ric]].values.push (data[j]); 
			}			   		
    	}
    	document.write(JSON.stringify(jsonData));   	
    }
	
</script>
</head>
<body>
	<input type="button" value="Build JSON data onclick" onclick="buildJSONData();">
</body>
</html>
  • Output:

2014-10-24 13_54_54-Create two dimensional array JSON JavaScript

Create One dimensional array JSON JavaScript selected Key

Run this code in Plunker here 

Leave a Reply

Your email address will not be published. Required fields are marked *