本文主要和大家分享angular中json对象push到数组中实例,希望能帮助到大家。
在项目中,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);
};
相关推荐:
javascript数组中关于push方法的注意事项
有关push的文章推荐10篇
javascript数组中push方法用法分析
以上就是angular中json对象push到数组中实例的详细内容。