如何在数组

时间:2017-11-02 09:46:35

标签: javascript jquery arrays jquery-ui

如何在数组中设置冒号。我刚刚用冒号连接。请帮我解决其他问题。



var data = [{"name":"a","id":"1"},{"name":"b","id":"2"}];
var obj = [];
$.each(data,function(k,v){
  obj.push(v.id + ":"+ v.name); 
});
  console.log(" obj is "+ JSON.stringify(obj));

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
&#13;
&#13;
&#13;

价值应该是 [&#34; 1:a&#34;,&#34; 2:b&#34;]

1 个答案:

答案 0 :(得分:0)

您需要创建对象,然后将其推送到数组中:

$.each(data,function(k,v){
 var jo = {};
 jo[v.id] = v.name;
 obj.push(jo); 
});