简体   繁体   English

将对象转换为数组(angularjs和typeahead)

[英]Converting object to array (angularjs and typeahead)

Let's say I have an object that looks like this: {"1": "2", "3": "4"} 假设我有一个如下所示的对象:{“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? 我没有直接访问这些数据,所以当我通过ajax将其转换为数组时如何? Like so: [{"1": "2"}, {"3": "4"}] 像这样:[{“1”:“2”},{“3”:“4”}]

PS: I'm using this output data in an angular-ui typeahead which dislikes objects and only likes strings. PS:我在角度ui类型中使用此输出数据,它不喜欢对象而只喜欢字符串。

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! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM