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

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

问题描述

我在这里遇到了一个心理负担问题,我使用 Backbone 中的集合检索了一个 JSON 对象.这是对象的样子:

<代码>{"MatchID": "00000001","日期": "1970-01-01T00:00:00.000Z","原始ID": "",阶段": {"StageNumber": "0","StageType": "舞台类型"},圆形的": {"RoundNumber": "0",名称":回合名称"},"腿": "1",主队": {"TeamID": "0",名称":主队名称"},客队":{"TeamID": "0",名称":客队名称"},地点":{"场地ID": "0",名称":维纳斯名称"},裁判":空,}

我想对这些数据做的是根据特定属性对其进行过滤,例如 Venue.Name 或 Date 属性(它们是对象的不同深度,对于某些其他数据).我在 Backbone 集合中有以下代码来过滤并返回一个新的集合,其中的内容经过适当过滤:

findWhere:函数(属性,值){返回新的 Project.Collections.Fixtures(this.filter(function (fixture)){return eval('fixture.attributes.' + Attribute) == Value;}));}

这允许我在属性中指定我想要过滤的属性,以及我希望它等于什么,对于任何深度的对象.问题是,我真的不想使用eval"来做到这一点,但显然我不能将[Attribute]"用于AwayTeam.TeamID"之类的东西,因为它不起作用.

有谁知道我可以在不使用 eval 的情况下实现此功能的方法吗?

解决方案

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

var x = {y:{z: 1}};函数 findprop(obj, path) {var args = path.split('.'), i, l;for (i=0, l=args.length; i

您可以将此作为方法添加到您的 Fixture 对象:

Fixture = Backbone.Model.extend({findprop:函数(路径){var obj = this.attributes,args = path.split('.'),我,l;for (i=0, l=args.length; i

并用它来提取值

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

findWhere 方法可以重写为

findWhere:函数(属性,值){返回新的 Project.Collections.Fixtures(this.filter(function (fixture){return fixture.findprop(Attribute) === Value;}));}

还有一个可以玩的小提琴 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天全站免登陆