dailyherold dailyherold - 1 year ago 39
Javascript Question

Script debugger confirms intended conditional check between JSON key and string, however I am getting an undesired result?

I am looking to modify the output table I am getting from this handy Json-to-HTML-Table script I stumbled upon here on SO. There is a point (line 86) where json-to-table.js passes a JSON object and generates array keys to be used as table headers. Optionally, this

array_key
function can generate only one key for a specified
search_value
parameter passed. I however [attempted] to modify it so that ALL array keys that did NOT match the
search_value
would be returned. Here is the function after my changes:

function array_keys(input, search_value, argStrict)
{
var search = typeof search_value !== 'undefined', tmp_arr = [], strict = !!argStrict, include = '', key = '';

if (input && typeof input === 'object' && input.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
return input.keys(search_value, argStrict);
}

for (key in input)
{
if (input.hasOwnProperty(key))
{
include = false;
if (search)
{
if (strict && input[key] == search_value)
include = false;
else if (input[key] == search_value)
include = false;
else
include = true;
}
if (include)
tmp_arr[tmp_arr.length] = key;
}
}
return tmp_arr;
}


Now, the reason I did this is because I want my generated table to not include a specific column from my JSON object:

{
"examItemCategories": [
{
"catgoryName": "01-Normal processes",
"catgoryPath": "General Area\\01-Normal processes",
"numberOfItems": 2,
"percentage": "6.06"
}
]
}


Given that I can not modify the original JSON obj passed, I was determining whether or not to attempt to modify the table post creation (e.g. remove column), or during. After looking at the
array_keys
function, I felt I could easily invert the conditional checking for the
search_value
.

I now call
array_keys(parsedJson[0], 'catgoryPath');
from json-to-table script. (Yes, catgoryPath is the correctly spelled name haha). Then I set a break point at the for loop within
array_keys
function to follow it through in Firebug.

First iteration:
catgoryName
is added to
tmp_arr
, 2nd iteration:
catgoryPath
is added to
tmp_arr
...and continues through 3rd and 4th iterations. I do not want
catgoryPath
added.

Looking at script debugger, on the 2nd iteration, whether or not
catgoryPath
gets added comes down to the conditional:
else if (input[key] == search_value)
line. The thing is, on the respective iteration both
key
and
search_value
variables equal "catgoryPath" according to Firebug. So therefore,
include = false;
should fire correct?

Any help is appreciated, apologies for the length and liberal usage of inline code.

Answer Source

Instead of using the array_keys function from the json-to-table script, if you are using JS > 1.8.5 you can use Object.keys(obj) to return an array of a given object's own enumerable properties.

The returned array of keys are then used as table headers under which the table populates with JSON data thanks to the script. Prior to the creation of table, I took my array of table headers and used array.splice(index, howMany) instead of delete (see here) to preserve my array index values.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download