1

I have an object like this that's generated from dynamic checkboxes in angular:

{"United States":true,"Canada":true,"Australia":false}

It works great as far as storing the values, but to be able to do database searches for the country values, I want to be able to convert it to an array to save it to the database (MongoDb/Mongoose) like:

["United States":true,"Canada":true,"Australia":false] 

And then convert it back to the exact same type of object for the application.

What would be the easiest way to do this?

6
  • I don't think ["United States":true,"Canada":true,"Australia":false] has the correct syntax for an array. Commented Dec 4, 2015 at 2:03
  • Underscore or Lodash: _.pairs(myObj) Assuming you want pairs Commented Dec 4, 2015 at 2:06
  • Well I don't necessarily need the :true part for the array, I would only need to store the true values... the most important thing is that I would be able to store it as an array and then be able to convert it back to the same object if I retrieved it from the database. Commented Dec 4, 2015 at 2:19
  • If you only store true values you change the number of values you're storing & could not accurately recreate the original object. Why can't you store the object in Mongo? mongodb.github.io/node-mongodb-native/api-articles/… Commented Dec 4, 2015 at 2:29
  • LukeP, using the _.pair(myObj) function seems easy enough in underscore. Do they have a similar command to convert the result back to an object? Commented Dec 4, 2015 at 2:32

2 Answers 2

1

I might interpolate the values as:

"string", boolean, "string", boolean, "string", boolean

To do this:

// Taking the object...
var countryObject = {"United States":true,"Canada":true,"Australia":false};

// ... and simplifying for explanation as:
var countryObject = {"A":true,"B":true,"C":false};

var compact = [];
for(var prop in countryObject){
    compact.push( prop );
    compact.push( countryObject[prop] );
}

// compact yields interpolated values as a basic array:
// ["A", true, "B", false, "C", true];

FYI: You can now search the array quite efficiently by finding the index of the string value and then adding 1 to retrieve the boolean value by adding 1:

var foundIndex = compact.indexOf("B");
var result;
if(foundIndex > -1){
    result = countryArray[foundIndex + 1];
}

To piece back together:

var compact = ["A", true, "B", false, "C", true];
var countryObject = {};
for(var i=0; i<compact.length; i+= 2){
    countryObject[compact[i]] = compact[i+1];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, your solution works like a charm.
1

What you're asking for doesn't really make sense because the array you demonstrated is an associative array with key:value pairs, which in JavaScript, is an object.

You can create an array and assign it key:value pairs like so:

var a = [];
a['test'] = 1;

However, that is just playing on the fact that arrays are objects in JavaScript.

1 Comment

Well do you know what would be a way to only store the countries that are true as an array, then convert it back to a similar object as the original when needed for the application?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.