Let's say I have an object that looks like this: {"1": "2", "3": "4"}
I don't have direct access to this data, so when I bring it in via ajax how can I convert it to an array? Like so: [{"1": "2"}, {"3": "4"}]
PS: I'm using this output data in an angular-ui typeahead which dislikes objects and only likes strings.
Here's a snippet:
var inputObj = {'1': '2', '3': '4'};
var output = [];
for (var key in inputObj) {
// must create a temp object to set the key using a variable
var tempObj = {};
tempObj[key] = inputObj[key];
output.push(tempObj);
}
console.log(output);
Hope that helps!
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