JSON:如何处理从键派生的属性?
我的应用程序中有一个这样的 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"
}
};
属性 file
和 url
始终可以从相应的密钥派生,因此我目前在我的应用程序中定义了上述对象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;
}
但是,由于 file
和 url
是派生属性,因此将它们添加到对象中似乎是多余的。但由于我传递的是每个页面的值,而不是键,所以我也必须将其添加到对象中,这也是多余的。像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该考虑将页面存储为对象列表而不是具有属性的对象。这在逻辑上看起来更加一致,并解决了您的冗余问题。
此外。您可以为页面对象创建类并使用计算派生属性的方法(可以选择缓存它们,以防您认为重复访问的成本很高。但是,对于简单的字符串连接而言,这样做似乎有些过头了)
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.
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)
当您已经以一种形式(您的密钥)拥有相同的数据时,为什么要以不同的形式存储相同的数据。
在我看来,不要选择其中任何一个,因为每当您希望获取特定页面的
file
和url
时,您都可以轻松地从 <代码>页面.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
andurl
for a particular page, you can easily get it from thepage.key
.