davout wrote:aliasmask wrote:Are you putting the objects in the array because you want to define the order, or will the objects have multiple keys? I'm thinking a simple object is good enough for key : result type table. Adding the extra array actually complicates it more and adds to execution speed when passing nested structures.
Because the data I'm working with is an array of objects. So rather than transforming my data everywhere I want display in chat as a table, I moved the logic here.
If there's a more efficient way, I'm all ears.
Well, it kind of depends on your data. The simplest way to convert a json array of objects to a single json object is to use regex removing all {}'s and []'s.
Code: Select all
[H: newObj = "{"+regex(oldObj,"[{}[]|]","")+"}"]
But if you have {}'s and []'s in your data, that won't work. You would resort to a loop to build the new object.
I was going to rewrite this to make it a bit more generic, but allow for style options. As is, this can't be posted to a chat window because of the style. I have another function that adds css styles and replaces class identifiers. For things like table or tr the tag will have to look like this <table class="table">. This basically allows the same code to either use a style sheet or not and since chat doesn't allow style sheets, this function works well for that.
So, what I propose is a function called tooltipResultTable where all the values of a json object are summed and totaled at the end. It will have a threshold option for failure or success with simple formatting (result in red for below, black for match, blue for above) and a text style option for the generic class names throughout the table. Callback just seems unrelated to text formatting and shouldn't be a part of function. Given a json object and values for the styles. This will replace the classes in the text, otherwise a style sheet can be used outside of the formatting of table either as a style sheet or an attached style tag.
Something like this:
tooltipResultTable(jObject[, threshold[, jStyles]]): formattedTooltip
I can also make this share the scope and define tooltipResultTable.value, although that's kind of hacky IMO. Best to be explicit in what values are returned. I'm sure someone has written this before, but having json.sum function separate is another good function. This would take either an array or object and sum all the values. I'll probably write that up too

The reason I think it's best to pass a simple json instead of an array of json objects is because of function speed. The more complex the object, the slower things get. Also, other than your needs because of existing data, there's no good reason to do this unless you're going to have separators in the table, like multiple headers and separated sums. But you can get around that too if you just check the json object's value. If text, then use as a header, which I may do as well. For purposes of styles, it would be header1, header2, ... because it would need to be dynamic. So a potential object could be:
Code: Select all
{"synergies":"Synergies","Synergy 1": 2,"Synergy 2": 0,"tempBonuses","Temporary","Temp Bonus 1":4,"Temp Bonus 2":2}
Output wrote:Synergies
Synergy 1 2
Synergy 2 0
Temporary
Temp Bonus 1 4
Temp Bonus 2 2
Json objects maintain their order with simple sets and I believe even with merging so you don't need to put objects in an array to maintain a specific order if that is important to the output. In the above example, it would be possible to return synergies and tempBonuses's sums as well, but I think that may be going outside the scope of a formatting function. Best to use a separate function for that.