对象数组 - 快速转换 [英] Object to array - quick conversion

查看:125
本文介绍了对象数组 - 快速转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的一个JSON结构:

I have a Json structure of this kind:

这是从后端(我用火力地堡)生成,并把一个变量:

this is generate from the backend (I'm using Firebase) and put in a variable :

var newJson = {}

我使用AngularJS和NG重复,虽然这个变量来显示结果 - 和它的作品。我倒要通过订购票财产JSON和我试图使用angularJS

I'm using AngularJS and ng-repeat though this variable to show the results - and it works. I d like to order the JSON by votes property and I have tried use the angularJS

| orderBy: 'vote' "

但是,这并不工作,因为我是NG-重复虽然JSON,而不是数组。
我曾尝试不同的解决方案表现出在 https://github.com/angular/angular.js/问题/ 1286
但我不能做任何工作,他们中的。

But that doesn't work cos I'm "ng-repeating" though a JSON and not array. I have tried different solutions showed at https://github.com/angular/angular.js/issues/1286 but I couldn't make work any of them.

所以我想,去的容易的道路,我的JSON转换为数组。
我已经试过:

So I thought to go for the easy road and converting my JSON to array. I ve tried :

 var arreyM = [];
 for (var i = 0; i < $scope.newJson.length; i++) {
      arreyM.push(name: $scope.newJson[i].name, pic: $scope.newJson[i].pic, vote:$scope.newJson[i].vote); 
 }

但它给了我错误 - 我猜的语法是错误的。

but it gave me error - I guess the syntax is wrong.

推荐答案

试试这个

var arreyM = [];
 for (var i = 0; i < $scope.newJson.length; i++) {
      arreyM[i]['name']= $scope.newJson[i].name;
      arreyM[i]['pic']= $scope.newJson[i].pic
      arreyM[i]['pic'] =  $scope.newJson[i].vote; 
 }

希望这将有助于

这篇关于对象数组 - 快速转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆