Javascript:将数组处理成对象(按角度)
Javascript : dealing with arrays into object (in angular)
我有以下问题:
$scope.creneaux1=creneauxFactory.list();
console.log($scope.creneaux)
给了我我所期望的:
Object {}
并且在对象内部
creneaux : Arrays[35]
inscriptions : Arrays[554]
等等。。
每当我尝试用访问铭文阵列时
console.log($scope.creneaux.inscriptions)
console.log($scope.creneaux.inscriptions[0])
console.log($scope.creneaux["inscriptions"])
我没有定义。
我该怎么办?
因此使用的工厂零件:
var creneaux ={},
urlphp = "http://bacly.fr/baclymphp/",
phpFiles = {
getCreneaux: "getCreneaux.php",
getInscriptionsclub: "getInscriptionsclub.php",
getUsers: "getUsers.php"
},
countResponse=0;
function getDate(from, onSuccess, onError) {
$http.get(urlphp + from).then(function (response) {
if (response) {
if (onSuccess) {
onSuccess(response)
}
} else if (onError) {
onError()
}
}, function () {
onError();
}
)
}
getDate(phpFiles.getCreneaux, function (response) {
creneaux.creneaux = response.data;
countResponse++;
}, function () {
alert("pas d acces reseau");
});
getDate(phpFiles.getInscriptionsclub, function (response) {
creneaux.inscriptions = response.data;
countResponse++;
}, function () {
alert("pas d acces reseau");
});
getDate(phpFiles.getUsers, function (response) {
creneaux.users = response.data;
countResponse++;
}, function () {
alert("pas d acces reseau");
});
return {
getResponseAfterSuccess: function (onSuccess, onError) {
if (Object.keys(phpFiles).length == countResponse) {
if (onSuccess) onSuccess(tournois);
} else {
if (onError) onError(tournois);
}
},
list: function(){
return creneaux;
},
listinsc: function(){
return creneaux.inscriptions;
},
findcreneau: function(cid){
return _.find(creneaux.creneaux, function(t) {return t.creneau_id === cid});
},
findinscription: function(cid){
return _.filter(creneaux.inscriptions, function(t) {return t.inscriptions_uid == cid});
},
更新:我试图改进我的代码,但当我使用例如:时
$scope.selectedinscription=creneauxFactory.findinscription(window.localStorage.getItem('logmbaclyuid'));
我得到一个空数组。如何继续等待数据可用?
您需要正确等待,直到收到所有数据,然后启动回调。这里有一种方法。
<div ng-app="myApp" ng-controller="testCtrl">
<pre>
{{selectedinscription|json}}
</pre>
</div>
// Factory
var app = angular.module('myApp', []);
app.factory('dataFactory', function ($http) {
var urlphp = "http://jsonplaceholder.typicode.com/",
phpFiles = {
getCreneaux: "posts",
getInscriptionsclub: "albums",
getUsers: "comments"
},
countResponse = 0;
function getDate(from, onSuccess, onError) {
$http.get(urlphp + from).then(function (response) {
if (response) {
if (onSuccess) {
onSuccess(response)
}
} else if (onError) {
onError()
}
}, function () {
onError();
})
}
return {
creneaux: {},
init: function (callback) {
var that = this;
getDate(phpFiles.getCreneaux, function (response) {
console.log(response);
that.creneaux.creneaux = response.data;
that.getResponseAfterSuccess(callback);
}, function () {
alert("pas d acces reseau");
});
getDate(phpFiles.getInscriptionsclub, function (response) {
console.log(response);
that.creneaux.inscriptions = response.data;
that.getResponseAfterSuccess(callback);
}, function () {
alert("pas d acces reseau");
});
getDate(phpFiles.getUsers, function (response) {
console.log(response);
that.creneaux.users = response.data;
that.getResponseAfterSuccess(callback);
}, function () {
alert("pas d acces reseau");
});
},
getResponseAfterSuccess: function (callback) {
if (_.keys(phpFiles).length === _.keys(this.creneaux).length) {
callback();
}
},
list: function () {
return this.creneaux;
},
listinsc: function () {
return this.creneaux.inscriptions;
},
findcreneau: function (cid) {
return _.find(this.creneaux.creneaux, function (t) {
return t.id === cid
});
},
findinscription: function (cid) {
console.log("Searching for " + cid);
console.log(this.creneaux);
return _.filter(this.creneaux.inscriptions, function (t) {
return t.id == cid
});
}
};
});
// Controller
app.controller('testCtrl', function ($scope, dataFactory) {
var onSuccess = function () {
$scope.selectedinscription = dataFactory.findinscription(1);
};
dataFactory.init(onSuccess);
});
这是一个正在工作的Fiddle。我已经使用了通用的RESTAPI来提取数据,但您明白了。
creneaux : Arrays[35]
inscriptions : Arrays[554]
如果它在$scope.creneaux中,那么输出意味着creneaux持有另一个creneaux数组,因此您可以这样访问它:
console.log($scope.creneaux.creneaux[0])
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 在Javascript中转换对象数组
- 在JavaScript中通过索引从对象数组中获取值
- Backbone虹吸以获取对象数组
- 如何在DataTables 2.1中迭代对象数组
- Javascript-根据赋值顺序,按键合并对象数组
- 将事件附加到对象/数组
- jQuery$.inArray()总是返回-1和一个对象数组
- javascript处理一个对象数组以获得一个新的对象数组
- javascript在数组中获取对象数组中键的所有不同值
- 在对象数组中查找多个值的d3范围
- Undercore.js获取对象数组中键对象的值
- 在mongoose中使用正则表达式在对象数组中进行查询搜索
- 如何通过json对象数组为嵌套对象赋值
- 如何循环通过2个对象数组并通过匹配id进行合并
- 为对象数组创建列表项
- 如何使用javascript合并两个对象数组
- JSON到对象数组,并向每个对象添加项
- JavaScript:从对象数组中获取唯一值及其计数
- 按不同项目对对象数组进行排序