Solved

issue with objects and arrays

Posted on 2014-04-09
6
231 Views
Last Modified: 2014-04-09
I have an array of names that I then want to convert into a list of the name and number of times it appears. The format of this JSON should be

{"myCounty":"Boone","totalCount":"5"}

however I am having issues generating that JSON

Below is what I currently have





<div id='myInfo1'>-</div>
<script>
a = ["Boone", "Story", "Polk", "Boone", "Story", "Boone"];
var data = {};
for (i = 0; i < a.length; ++i) {
    if (!data[a[i]]) data[a[i]] = 0;
    ++data[a[i]];
}

b = JSON.stringify(data);

$('#myInfo1').html(b);

</script>

Open in new window

0
Comment
Question by:lvmllc
  • 3
  • 2
6 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
What are your issues?
Try putting your $('$myInfo1').html(b) in a document ready block i.e
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
</head>
<body>

<div id='myInfo1'>-</div>
<script>
a = ["Boone", "Story", "Polk", "Boone", "Story", "Boone"];
var data = {};
for (i = 0; i < a.length; ++i) {
    if (!data[a[i]]) data[a[i]] = 0;
    ++data[a[i]];
}

b = JSON.stringify(data);
$(function() {
  $('#myInfo1').html(b);
});

</script>
</body>
</html>

Open in new window

0
 
LVL 5

Expert Comment

by:jayakrishnabh
Comment Utility
the code given works fine. I think $('#myInfo1') may not be having any html (not loaded/rendered) if this is executed on load. Please execute the same on a button click function where $('#myInfo1') would be available. It worked for me that way.
0
 

Author Comment

by:lvmllc
Comment Utility
Sorry, I should have made my question and example more clear so let me try this again.

Documentation is in file below but what I need to generate is JSON in this format as my output and I am not sure how to do that when I count how many time sa county name shows up in the array. Note that the actual array data I am receiving has 100's of county names but for for now this small set works.

[{"myCounty":"Boone","totalCount":"3"},
{"myCounty":"Story","totalCount":"2"},
{"myCounty":"Polk","totalCount":"1"}]

Open in new window



<!doctype html>
<html>
<head>
<title>object issue</title>
<script src="http://code.jquery.com/jquery.js"></script>
</head>
<body>
Need to create a JSON data in this format <br>[{"myCounty":"Boone","totalCount":"3"},<br>
{"myCounty":"Story","totalCount":"2"},<br>
{"myCounty":"Polk","totalCount":"1"}]<br>
<br>
<br>
But I am getting this and note sure how to change it to the above format where there are two sets of name/value pairs.
<div id='myInfo1'>-</div>
<script>

$(document).ready(function () {


a = ["Boone", "Story", "Polk", "Boone", "Story", "Boone"];
var data = {};
for (i = 0; i < a.length; ++i) {
    if (!data[a[i]]) data[a[i]] = 0;
    ++data[a[i]];
}

b = JSON.stringify(data);
$(function() {
  $('#myInfo1').html(b);
});

})

</script>
</body>
</html>

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Do you mean something like this?
BTW your code has a document ready block inside a document ready block - you can loose the outer document.ready.
<script>
a = ["Boone", "Story", "Polk", "Boone", "Story", "Boone"];
var data = {};
var data2 = [];
for (i = 0; i < a.length; ++i) {
    if (!data[a[i]]) data[a[i]] = 0;
    ++data[a[i]];
}
var i =0;
for (var key in data) {
  data2[i] = {};
  data2[i].myCounty = key;
  data2[i].totalCount = data[key];
  i++;
}

b = JSON.stringify(data2);
$(function() {
  $('#myInfo1').html(b);
});
</script>

Open in new window

0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
Here is an alternative
<script>
a = ["Boone", "Story", "Polk", "Boone", "Story", "Boone"];
// SORT THE ARRAY SO LIKE COUNTIES ARE CONTIGUOUS
a.sort();
var data=[];
var indx = -1;
// KEEP TRACK OF WHICH COUNTY WE ARE PROCESSING
var current = '';
// CREATE THE FIRST OBJECT ELEMENT
data[0] = {};
for(i=0; i< a.length;i++)
{
    // IF NEW COUNTY THEN 
  if (a[i] != current) {
      // CREATE NEXT ITEM IN OUTPUT ARRAY
    // AND INITIALISE
    indx++;
    data[indx] = {};
    data[indx].myCounty = a[i];
    data[indx].totalCount = 0;
    // SET THE CURRENT TO NEW COUNTY
    current = a[i];
  }
  // INCREMENT THE COUNT
  data[indx].totalCount++;
}

b = JSON.stringify(data);
$(function() {
  $('#myInfo1').html(b);
});
</script>

Open in new window

0
 

Author Closing Comment

by:lvmllc
Comment Utility
Works great!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now