Link to home
Start Free TrialLog in
Avatar of lvmllc
lvmllcFlag for United States of America

asked on

issue with objects and arrays

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

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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

Avatar of jayakrishnabh
jayakrishnabh

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.
Avatar of lvmllc

ASKER

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

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

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lvmllc

ASKER

Works great!