Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

issue with objects and arrays

Posted on 2014-04-09
6
Medium Priority
?
242 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 60

Expert Comment

by:Julian Hansen
ID: 39988801
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
ID: 39988809
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
ID: 39989146
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39989636
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 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 39989665
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
ID: 39989884
Works great!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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 the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…
Suggested Courses

971 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