It seems like I may have approached this task incorrectly, perhaps due to mishandling recursion. I'm unsure of where the mistake lies. You can view the example here.
Below is the JavaScript code snippet -
function propertyTest(currentObject, key) {
for (var property in currentObject) {
if (typeof currentObject[property] === "object") {
propertyTest(currentObject[property], property);
} else {
// Testing output only
$('#method1').append((property == 'value' && key ? key : property) + ' -- ' + currentObject[property] + '<br />');
propertyKey = (property == 'value' && key ? key : property);
propertyValue = currentObject[property];
var arrayJSON = [];
arrayJSON.push(propertyKey);
arrayJSON.push(propertyValue);
}
}
var JSONString = JSON.stringify(arrayJSON);
console.log(JSONString);
return JSONString;
}
Here is the original JSON data structure -
// JSON data objects go here
The JSON data is passed to the function as follows -
propertyTest(oarsObject);
This snippet from the console shows where things seem to deviate -
// Console log details displayed here
There's inconsistency in the results and it appears that my recursive method might be flawed.
While JavaScript arrays typically require numerical keys, JSON.stringify()
works differently on various data elements. The ultimate goal is to create small, individual JSON strings from this process. The desired output should consist of distinct key-value pairs similar to the example provided.
I seem to be struggling with extracting and formatting the pairs correctly rather than constructing the entire JSON string. Is creating an object instead of an array a potential solution? Or could there be other issues within my recursive approach?