在 JSON 对象上使用 jQuery 的 find()
类似于 brnwdrng 的问题,我正在寻找一种方法来搜索类似 JSON 的对象。
假设我的对象的结构是这样的:
TestObj = {
"Categories": [{
"Products": [{
"id": "a01",
"name": "Pine",
"description": "Short description of pine."
},
{
"id": "a02",
"name": "Birch",
"description": "Short description of birch."
},
{
"id": "a03",
"name": "Poplar",
"description": "Short description of poplar."
}],
"id": "A",
"title": "Cheap",
"description": "Short description of category A."
},
{
"Product": [{
"id": "b01",
"name": "Maple",
"description": "Short description of maple."
},
{
"id": "b02",
"name": "Oak",
"description": "Short description of oak."
},
{
"id": "b03",
"name": "Bamboo",
"description": "Short description of bamboo."
}],
"id": "B",
"title": "Moderate",
"description": "Short description of category B."
}]
};
我想得到一个 id="A" 的对象。
我尝试过各种方法,例如:
$(TestObj.find(":id='A'"))
但似乎没有任何效果。
任何人都可以想出一种基于某些标准检索项目而不使用“each”的方法吗?
Similar to brnwdrng's question, I'm looking for a way to search through a JSON-like object.
supposing my object's structure is like so:
TestObj = {
"Categories": [{
"Products": [{
"id": "a01",
"name": "Pine",
"description": "Short description of pine."
},
{
"id": "a02",
"name": "Birch",
"description": "Short description of birch."
},
{
"id": "a03",
"name": "Poplar",
"description": "Short description of poplar."
}],
"id": "A",
"title": "Cheap",
"description": "Short description of category A."
},
{
"Product": [{
"id": "b01",
"name": "Maple",
"description": "Short description of maple."
},
{
"id": "b02",
"name": "Oak",
"description": "Short description of oak."
},
{
"id": "b03",
"name": "Bamboo",
"description": "Short description of bamboo."
}],
"id": "B",
"title": "Moderate",
"description": "Short description of category B."
}]
};
I'd like to get an object with id="A".
I've tried all sort of stuff such as:
$(TestObj.find(":id='A'"))
but nothing seems to work.
Can anyone think of a way of retrieving an item based on some criteria without using 'each'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
jQuery 不适用于普通对象文字。您可以以类似的方式使用下面的函数来搜索所有“id”(或任何其他属性),无论其在对象中的深度如何:
像这样使用:
jQuery doesn't work on plain object literals. You can use the below function in a similar way to search all 'id's (or any other property), regardless of its depth in the object:
Use like so:
纯 javascript 解决方案更好,但 jQuery 方法是使用 jQuery grep 和/或 map 方法。
可能并不比使用 $.each
或
返回匹配对象的数组或在地图的情况下查找值的数组好多少。只需使用这些也许就能做你想做的事。
但在这个示例中,您必须执行一些递归,因为数据不是平面数组,并且我们接受任意结构、键和值,就像纯 JavaScript 解决方案一样。
本质上与 Box9 的答案相同,但在有用的地方使用 jQuery 实用函数。
·······
The pure javascript solution is better, but a jQuery way would be to use the jQuery grep and/or map methods.
Probably not much better than using $.each
or
Returns an array of the matching objects, or of the looked-up values in the case of map. Might be able to do what you want simply using those.
But in this example you'd have to do some recursion, because the data isn't a flat array, and we're accepting arbitrary structures, keys, and values, just like the pure javascript solutions do.
Essentially the same as Box9's answer, but using the jQuery utility functions where useful.
········
这对我有用 [{"id":"data"},{"id":"data"}]
This works for me on [{"id":"data"},{"id":"data"}]
对于一维 json 你可以使用这个:
For one dimension json you can use this:
您可以使用 JSONPath
执行以下操作:
请注意,JSONPath 返回一个数组结果
(我还没有测试表达式“$..[?(@.id=='A')]”顺便说一句。也许它需要在浏览器控制台的帮助下进行微调)
You can use JSONPath
Doing something like this:
Note that JSONPath returns an array of results
(I have not tested the expression "$..[?(@.id=='A')]" btw. Maybe it needs to be fine-tuned with the help of a browser console)
我想提到的另一个选项是,您可以将数据转换为 XML,然后按照您想要的方式使用 jQuery.find(":id='A'")。
有一些 jQuery 插件可以实现此效果,例如 json2xml。
可能不值得转换开销,但这对于静态数据来说是一次性成本,因此它可能很有用。
Another option I wanted to mention, you could convert your data into XML and then use
jQuery.find(":id='A'")
the way you wanted.There are jQuery plugins to that effect, like json2xml.
Probably not worth the conversion overhead, but that's a one time cost for static data, so it might be useful.