Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting object to array (angularjs and typeahead)

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.

like image 438
matenji Avatar asked Mar 29 '26 23:03

matenji


1 Answers

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!

like image 143
SteveD Avatar answered Apr 02 '26 10:04

SteveD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!