在项目中,api要求的数据格式为
$scope.data = { "name":"zhangsan", "menus": [{"id":1},{"id":2}] }
而我的返回格式为
$scope.data=["name":"zhangsan"] $scope.selected = [1,2,3];
需要将两个数组整合,其中$scope.selected要先转化为json对象,再进行push操作。
代码如下:
// 将menu数组转化为json格式 self.convertjson = function (callback) { //传入数组为$scope.selected,每循环一遍就push一次 angular.foreach($scope.selected, function (value, key) { $scope.data.menus.push({ 'id':value }); }); callback($scope.data); };
以上这篇angular json对象push到数组中的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
解决三元运算符 报错“SyntaxError: can''t assign to conditional expression”
解决angular 使用原生拖拽页面卡顿及表单控件输入延迟问题
使用 Angular RouteReuseStrategy 缓存(路由)组件的实例代码
分享Angular http interceptors 拦截器使用(推荐)
网友评论