1

So to GET an output like this, I had to use some pretty cool tricks (see Analyzing Data from JSON in JavaScript). Please note my data in this example (# rows) is different.

var feeds = [].concat.apply([], dataSet_parsed.map(s => s.data));

//flattens arrays AGAIN to show all targets 
var targets = [].concat.apply([], feeds.map(s => s.data));

//return target hits total
    var targetList = targets.reduce(
        function(prev, cur) {
            var val = cur["val"];
            prev[val] = ((prev[val] || 0) + 1);
            return prev;
        }, {});

// Output: {TargetA: 5, TargetB: 6, TargetC: 4, TargetD: 2}

Basically, I'm trying to get a count of how many times a target was seen per group. And fantastic! this works.

Here's my question. how do I display the output---

{TargetA: 5, TargetB: 6, TargetC: 4, TargetD: 2}

---in a table? There are no guarantees that I will always return the same TYPE of Targets (i.e. next load I could have:

{TargetK: 10, TargetL: 2, TargetM: 5, TargetN: 3, TargetO: 7, TargetP: 8}

I've tried using JSON.stringify and .replace() but I'm not getting very far. And even after that, I don't think I could style that output very well.

JSON.stringify(targetList).replace(/\}/g,'').replace(/\{/g,'')
KateJean
  • 428
  • 5
  • 17
  • Do you mean you want to use `targets` to generate an HTML string such as `
    Target>/th>Count
    TargetA5
    TargetB6
    TargetC4
    TargetD2
    `? Also, do you care about the order of the rows?
    – David Knipe Feb 23 '18 at 19:51
  • yes, exactly. and order doesn't matter necessarily. it'd be a nice-to-have – KateJean Feb 23 '18 at 19:54
  • 1
    Oh, and make sure you escape the target names properly. For example, if a malicious user creates a target called ``, then they can access the session cookies of other users who run your client-side code by listening on `http://www.my-malicious-site:666/harvest-cookies`. https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript discusses this, and annoyingly javascript doesn't seem to have a built-in way of doing it nicely. – David Knipe Feb 23 '18 at 20:33

0 Answers0