解析$ http结果以获取AngularJS中的特定项目

戴维·D。

我从$http GETangularJS中获得了json样式的结果看起来像这样:

{
    "meta":{
    "limit":1000,
},
"objects":[
    {
        "custom_pk":"1",
        "linked_client":null,
        "resource_uri":"/api/v1/card_infos/1"
    },
    {
        "custom_pk":"2",
        "linked_client":null
    }, ...

我想要一个包含所有custom_pk的数组,以执行以下操作:

$scope.validate_pk = function(val){
    if (val in myArray)
        // do some stuff

您如何创建myArray?

达林·迪米特罗夫(Darin Dimitrov)

您可以像这样提取对象:

var json = ... the javascript object shown in your question
var custom_pks = [];
for (var i = 0; i < json.objects.length; i++) {
    custom_pks.push(json.objects[i].custom_pk);
}
// at this stage the custom_pks array will contain the list of all
// custom_pk properties from the objects property

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章