Solved

issue with objects and arrays

Posted on 2014-04-09
6
235 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 54

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 54

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 54

Accepted Solution

by:
Julian Hansen earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

777 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