使用数组表示法从JSON对象的多个级别的属性检索 [英] Multiple level attribute retrieval using array notation from a JSON object

查看:145
本文介绍了使用数组表示法从JSON对象的多个级别的属性检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在这里精神上征税问题,在这里我有在骨干网使用集合检索的JSON对象。这是该对象是什么样子:

  {
    MatchID:00000001,
    日期:1970-01-01T00:00:00.000Z
    OriginalID:,
    舞台:{
        StageNumber:0,
        StageType:台型
    },
    圆:{
        RoundNumber:0,
        名称:圆名
    },
    腿:1,
    HomeTeam:{
        TeamID:0,
        名称:主队名
    },
    AwayTeam:{
        TeamID:0,
        名称:客场球队名
    },
    场地:{
        VenueID:0,
        名:VENU名
    },
    裁判:空,
}

我想与此数据做什么,是基于特定属性,诸如Venue.Name或日期属性(它们是不同的深度到对象过滤它,并且可以是对于一些深于两个级别其他数据)。我有一个主干内部集合以下code过滤和筛选合适的内容返回一个新的集合:

  findWhere:功能(属性,值)
{
    返回新Project.Collections.Fixtures(this.filter(功能(夹具)
    {
        返回的eval('fixture.attributes。+属性)==值;
    }));
}

这让我在哪个属性我想过滤由属性指定的,我希望它是等于为对象的任何深度的东西。问题是,我真的不希望使用EVAL要做到这一点,但显然我不能用[属性]的东西,如AwayTeam.TeamID,因为这将无法正常工作。

有谁知道一个方法,我可以使用,无需使用eval来实现这一功能呢?


解决方案

这样的事情会让你遍历对象的层次结构,以找到一个值:

  VAR X = {
    Y:{
        Z:1
    }
};功能findprop(OBJ,路径){
    变参= path.split('。'),I,L;    对于(i = 0,1 = args.length; I<升;我++){
        如果(!obj.hasOwnProperty(参数[I]))
            返回;
        OBJ = OBJ [参数[I]];
    }    返回OBJ;
}findprop(X,'y.z');

您可以添加这是一个方法来你的灯具对象:

 夹具= Backbone.Model.extend({
    findprop:功能(路径){
        VAR OBJ = this.attributes,
            ARGS = path.split('。'),
            I,L;        对于(i = 0,1 = args.length; I<升;我++){
            如果(!obj.hasOwnProperty(参数[I]))
                返回;
            OBJ = OBJ [参数[I]];
        }
        返回OBJ;
    }
});

和用它来提取值

 变种F =新灯();
f.findprop(HomeTeam.TeamID);

findWhere 方法,然后可以改写为

  findWhere:功能(属性,值)
{
    返回新Project.Collections.Fixtures(this.filter(功能(夹具){
        返回fixture.findprop(属性)===值;
    }));
}

和一拨弄 http://jsfiddle.net/nikoshr/wjWVJ/3/

I've got a mentally taxing problem here, where I've got a JSON object retrieved using a collection in Backbone. This is what the object looks like:

{
    "MatchID": "00000001",
    "Date": "1970-01-01T00:00:00.000Z",
    "OriginalID": "",
    "Stage": {
        "StageNumber": "0",
        "StageType": "Stage Type"
    },
    "Round": {
        "RoundNumber": "0",
        "Name": "Round Name"
    },
    "Leg": "1",
    "HomeTeam": {
        "TeamID": "0",
        "Name": "Home Team Name"
    },
    "AwayTeam": {
        "TeamID": "0",
        "Name": "Away Team Name"
    },
    "Venue": {
        "VenueID": "0",
        "Name": "Venu Name"
    },
    "Referee": null,
}

What I want to do with this data, is filter it based on a particular attribute, such as the Venue.Name or Date attributes (which are different depths into the object, and can be deeper than two levels for some of the other data). I've got the following code inside a Backbone collection to filter and return a new collection with the contents filtered appropriately:

findWhere: function (Attribute, Value)
{
    return new Project.Collections.Fixtures(this.filter(function (fixture)
    {
        return eval('fixture.attributes.' + Attribute) == Value;
    }));
}

This allows me to specify in an attribute which attribute I want to filter by, and what I want it to be equal to, for any depth of object. The problem is, I really don't want to use "eval" to do this, but obviously I can't use "[Attribute]" for something like "AwayTeam.TeamID", as it won't work.

Does anyone know of a method I can use to achieve this functionality without using eval?

解决方案

Something like this would let you traverse the hierarchy of objects to find a value:

var x = {
    y: {
        z: 1
    }
};

function findprop(obj, path) {
    var args = path.split('.'), i, l;

    for (i=0, l=args.length; i<l; i++) {
        if (!obj.hasOwnProperty(args[i]))
            return;
        obj = obj[args[i]];
    }

    return obj;
}

findprop(x, 'y.z');

You could add this as a method to your Fixtureobject:

Fixture = Backbone.Model.extend({
    findprop: function(path) {
        var obj = this.attributes,
            args = path.split('.'), 
            i, l;

        for (i=0, l=args.length; i<l; i++) {
            if (!obj.hasOwnProperty(args[i]))
                return;
            obj = obj[ args[i] ];
        }
        return obj;
    }
});

and use it to extract the value

var f = new Fixture();
f.findprop("HomeTeam.TeamID");

The findWhere method could then be rewritten as

findWhere: function (Attribute, Value)
{
    return new Project.Collections.Fixtures(this.filter(function (fixture){
        return fixture.findprop(Attribute) === Value;
    }));
}

And a Fiddle to play with http://jsfiddle.net/nikoshr/wjWVJ/3/

这篇关于使用数组表示法从JSON对象的多个级别的属性检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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