1

I am not sure how to for this question correctly to get where I want to go. I have a JavaScript that runs well. It returns a huge set of data that I need to group to display in a more readable way.

sample code

        function parsePData(data) {

            var xmlDoc = $j.parseXML(data.d);
            var $xml = $j(xmlDoc);
            var items = $xml.find("Page");
            userFavorites = $xml;

            var newHtml = ""

            for (var i = 0; i < items.size(); i++) {

              newHtml += "<div style='margin:5px; padding:5px; border-bottom: 1px dotted #9C9C9C; font-  size:95%'><div> <b>#" + (i + 1) + ".</b>Item to Publish: " + items[i].getAttribute("Title") +

                "</div><div class='itpPub'> Publication Target: " + items[i].parentNode.getAttribute("Title") +

                "</div></div>";
            }

I need some pointers or directions to relevant posts on how to go about grouping this output. Apologies if i have missed the obvious posting.

The code creates the following output:

Item to Publish: a , Publication Target: 1
Item to Publish: a , Publication Target: 2
Item to Publish: b , Publication Target: 7

and I need to get to:

Item to Publish: a , Publication Target: 1, 2
Item to Publish: b , Publication Target: 7
1
  • You might want to show how you're calling that function, and (some specimen) data you're having it work with. Incidentally, running that code the only thing I get is in the console, reporting an error: 'unexpected end of input' Commented Sep 24, 2014 at 22:22

2 Answers 2

1

So reading through your post, it sounds like you need to pre-process your data before you attach it to the DOM:


Output you get:

Item to Publish: a , Publication Target: 1

Item to Publish: a , Publication Target: 2

Item to Publish: b , Publication Target: 7


Output you want:

Item to Publish: a , Publication Target: 1, 2

Item to Publish: b , Publication Target: 7


We can use JS object literal's key/value pairing to help accomplish this. To account for multiple values, we store an array containing values into the key/value pair.

Since I don't have access to what your items array actually returns, I just created some test data to show how it should work. JSFIDDLE: http://jsfiddle.net/biz79/pLb38t0y/

Hopefully you can adjust it to fit your purpose.

// create storage object literal and key/value vars
var storage = {};
var key,value;

// some test data
var items = [
  { "Title" : "a", "Value" : "1" },
  { "Title" : "a", "Value" : "2" },
  { "Title" : "b", "Value" : "3" } 
];

// iterate items array and populate storage
for (var i =0; i<items.length; i++) {
  key = items[i].Title;
  value = items[i].Value;

  // if the key exists in storage, add to it
  if ( key in storage ) {
    storage[key].push( value );
  }
  else {
  // if the key doesn't exist, create key and add to it
    storage[key] = [];
    storage[key].push(value);
  }
}

// iterate storage for answers
for (var prop in storage) {
    if (storage.hasOwnProperty(prop)){
      alert("Item to Publish: " + prop);
      alert("Publication Target: " + storage[prop].join(','));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

For cases like this I like to use the fact that object properties are unique. I've tried not to modify your code too much. I introduced an object, groupedItems, and used its properties for the group titles. In each of those titles I stored an array with the titles of the group.

The output is more or less the same. In a new loop (a for..in loop to iterator over the properties), you can output the key (= propety name = group title), and the joined values in the array value of that property.

function parsePData(data) {

            var xmlDoc = $j.parseXML(data.d);
            var $xml = $j(xmlDoc);
            var items = $xml.find("Page");
            userFavorites = $xml;

            // Create an object to cotnain the titles.
            var groupedItems = {};
            
            for (var i = 0; i < items.size(); i++) {
              // If the grouped object doesn't contain this title yet, add it and initialize to
              // and empty array.
              var prop = items[i].getAttribute("Title");
              if (! groupedItems.hasOwnProperty(prop))
                groupedItems[prop] = new Array();

              // Push new item onto the array.
              groupedItems[prop].push(items[i].parentNode.getAttribute("Title"));
            }
  
            var newHtml = ""
  
            // Actual output using the grouped array.
            for (var k in groupedItems)
            {
                newHtml += "<div style='margin:5px; padding:5px; border-bottom: 1px dotted #9C9C9C; font-  size:95%'><div> <b>#" + (i + 1) + ".</b>Item to Publish: " + k +

                "</div><div class='itpPub'> Publication Target: " +
                  groupedItems[k].join(',') +

                "</div></div>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.