JSON:如何处理从键派生的属性?

发布于 2024-11-03 13:18:54 字数 1164 浏览 5 评论 0原文

我的应用程序中有一个这样的 JSON 对象:

var pages = {
    home: {
        title: "Home",
        description: "The home page",
        file: "home.html",
        url: "/home"
    },
    blog: {
        title: "Blog",
        description: "Our blog",
        file: "blog.html",
        url: "/blog"
    }
};

属性 fileurl 始终可以从相应的密钥派生,因此我目前在我的应用程序中定义了上述对象code:

var pages = {
    home: {
        title: "Home",
        description: "The home page"
    },
    blog: {
        title: "Blog",
        description: "Our blog"
    }
};

$.each(pages, function(key, value) {
    value.file = key + ".html";
    value.url = "/" + key;
}

但是,由于 fileurl 是派生属性,因此将它们添加到对象中似乎是多余的。但由于我传递的是每个页面的值,而不是键,所以我也必须将其添加到对象中,这也是多余的。像这样:

var pages = {
    home: {
        title: "Home",
        description: "The home page"
    },
    blog: {
        title: "Blog",
        description: "Our blog"
    }
};

$.each(pages, function(key, value) {
    value.jsonKey = key;
}

现在我有三种不同的方法,但我并不喜欢其中任何一种。我认为这应该是一个相当普遍的问题,那么你会如何解决这个问题?如果派生属性要多次使用怎么办?

I have a JSON object like this in my application:

var pages = {
    home: {
        title: "Home",
        description: "The home page",
        file: "home.html",
        url: "/home"
    },
    blog: {
        title: "Blog",
        description: "Our blog",
        file: "blog.html",
        url: "/blog"
    }
};

The properties file and url can always be derived from the respective key, so I currently define the above object like this in my code:

var pages = {
    home: {
        title: "Home",
        description: "The home page"
    },
    blog: {
        title: "Blog",
        description: "Our blog"
    }
};

$.each(pages, function(key, value) {
    value.file = key + ".html";
    value.url = "/" + key;
}

However, since file and url are derived attributes, adding them to the object seems redundant. But since I pass the value around for each page, not the key, I would have to add it to the object as well, which would also be redundant. Like this:

var pages = {
    home: {
        title: "Home",
        description: "The home page"
    },
    blog: {
        title: "Blog",
        description: "Our blog"
    }
};

$.each(pages, function(key, value) {
    value.jsonKey = key;
}

Now I have three different approaches and don't really like any of those. I think this should be a fairly common problem, so how would you approach this? And what if the derived attribute is to be used more than once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-11-10 13:18:54

您应该考虑将页面存储为对象列表而不是具有属性的对象。这在逻辑上看起来更加一致,并解决了您的冗余问题。

var pages = [
    {
        key: 'home'
        title: "Home",
        description: "The home page",
    },
    {
        key: 'blog',
        title: "Blog",
        description: "Our blog",
    }
];

此外。您可以为页面对象创建类并使用计算派生属性的方法(可以选择缓存它们,以防您认为重复访问的成本很高。但是,对于简单的字符串连接而言,这样做似乎有些过头了)

you should consider storing pages as a list of objects rather than as an object with properties. This seems more consistent logically and solves your redundancy concerns.

var pages = [
    {
        key: 'home'
        title: "Home",
        description: "The home page",
    },
    {
        key: 'blog',
        title: "Blog",
        description: "Our blog",
    }
];

additionally. you can create classes for page objects and use methods that compute the derived properties (optionally caching them, in case you think repeated access is costly. However, doing that for simple string concatenation seems like an overkill)

断肠人 2024-11-10 13:18:54

当您已经以一种形式(您的密钥)拥有相同的数据时,为什么要以不同的形式存储相同的数据。
在我看来,不要选择其中任何一个,因为每当您希望获取特定页面的 fileurl 时,您都可以轻松地从 <代码>页面.key。

Why do you want to store the same data in a different form, when you already have it in one form(your key).
In my opinion, don't go for any of these, because whenever you wish to get the file and url for a particular page, you can easily get it from the page.key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文