Solved

issue with objects and arrays

Posted on 2014-04-09
6
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 58

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 58

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 58

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

636 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