I am dealing with some JSON data that I am retrieving from a database. If the result contains a single value, it creates a single object. If there are multiple values, it creates an array of objects.
My issue is that having to try and handle this becomes a problem when dealing with loops.
Example Data:
// Single result from DB
var obj = {
"records": {
"recordID": 1,
"recordName": 'test'
}
}
// Multiple results from DB
var obj = {
"records": [{
"recordID": 1,
"recordName": 'test'
}, {
"recordID": 2,
"recordName": 'test again'
}]
}
I have a function that loops over all of the records for example and this becomes problematic when we only have one result because we are no longer looping over an array.
Due to some of my objects being pretty large, I am trying to come up with a function that I can initialize my object with when I get it back from the database before handling it.
This function would loop through all of the keys and check to see if the key exists in an array of "Does this need to be an array?" flags. If it finds a match, check to see if its a single object and if so, convert it to an array of that single object.
Here is some pseudo code of what I am trying to do:
// Input
var obj = {
"records": {
"recordID": 1,
"recordName": 'test'
},
"photos": {
"photoID": 1,
"photoName": 'test flower'
},
"leaveMeAlone": {
"nopeID": 1,
"nopeName": 'tester'
}
}
function convertToArray(obj) {
var keysToArray = ['records', 'photos'];
// Loop over keys
for (var k in obj) {
// Properties
if (obj.hasOwnProperty(k)) {
// This key is in our array.
if (keysToArray.indexOf(k) > -1) {
// If this is a single object, turn it into an array containing a single object
if (!Array.isArray(obj[k])) {
// Turn this value into an array of the single object
/* Stuck Here */
}
}
}
}
/* Return
var obj = {
"records": [{
"recordID": 1,
"recordName": 'test'
}],
"photos": [{
"photoID": 1,
"photoName": 'test flower'
}],
"leaveMeAlone": {
"nopeID": 1,
"nopeName": 'tester'
}
}
*/
}
// run
convertToArray(obj);
You can use below method which I created. It will check if object is an array or not. If not then it will put object inside an array and will return.
function convertToArray(obj) {
if (obj.records instanceof Array) {
return obj.records;
} else {
return [obj.records];
}
}
JSFiddle https://jsfiddle.net/r4otdrq0/
You could take an iterative and recursive approach by converting given properties to an array, if not already an array.
If all keys are unique, you could use an early return by deleting the properties of the array, or changing the the path from depth first to breadth first search.
function convert(object, keys) {
Object.keys(object).forEach(function (k) {
if (object[k] && typeof object[k] === 'object') {
convert(object[k], keys);
}
if (keys.indexOf(k) !== -1 && !Array.isArray(object[k])) {
object[k] = [object[k]];
}
});
}
var object = { records: { recordID: 1, recordName: 'test' }, photos: { photoID: 1, photoName: 'test flower' }, leaveMeAlone: { nopeID: 1, nopeName: 'tester' }, nested: { convert: { foo: 1, bar: 2 } } },
keys = ['records', 'photos', 'convert'];
convert(object, keys);
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With