使用 JQuery 操作 JSON 对象
我们可以使用JQuery函数来操作和搜索JSON对象吗? 就像如果我有一个该对象的数组类型的大对象:
Node
{
Name,
Property1,
Property2
}
我可以使用 jquery 函数 find 来查找属性名称为 John 的节点吗? 并同样修改内容?
编辑:是的,我实际上正在寻找类似 JLinq 的东西, 谢谢
Can we use JQuery functions to manipulate and search in JSON Object?
Like if I have a big Object of type of array of this object:
Node
{
Name,
Property1,
Property2
}
can I use jquery function find to find a node with property Name as John?
and similarly modify the contents as well??
edit: yes, I was actually looking for something like JLinq,
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您在这里寻找的是 jLinq。它类似于 linq,但它是一个 jquery 插件。做你所要求的事情真的很容易。它会是这样的:
如果你只想要第一场比赛,请尝试:
这就是全部。非常快速、高效,而且非常语义化,因此很容易保持和理解意图。
i think what you're looking for here is jLinq. its like linq, but its a jquery plugin. to do what you're asking about is really easy. it would be something like :
if you want only the first match try :
and thats all there is to it. very quick and efficient, and very semantic, so its easy to maintain an understand the intent.
要找到该节点,您可以像这样循环...
这是 O(n)。
To find that node, you would loop through like so...
This is O(n).
不,抱歉。 jQuery 旨在处理 DOM 节点或 XML 结构。如果您想搜索对象哈希,您需要手动执行。即使 jQuery 有方法可以做到这一点,它也没有任何“魔法”可以让事情变得更快,就像它在 DOM 搜索中所做的那样——没有比递归搜索哈希更快的方法了(除非你已经预先解析过了)
天哪,人们!!
看,jQuery 并不是所有 JavaScript 的终极救世主。有些事情在直接 JS 中会更好!有什么可怕的:
你“寻找”的答案很简单:
快乐?它会变慢,因为你有嵌套的函数调用,它会变慢,因为它将迭代每个元素而不是停止。
但它使用 jQuery。
No, sorry. jQuery is meant for working with DOM nodes, or XML structures. If you want to search object hashes you need to do it manually. Even if jQuery had methods to do it, there isn't any "magic" that it can do to make things faster like it does with DOM searches -- there simply are no faster ways to search a hash than doing it recursively (unless you've pre-parsed it)
OMFG PEOPLE!!
Look, jQuery is not the be-all-end-all savior of everything JavaScript. Some things are just better in straight JS! What is so horrible about:
The answer you are "looking" for is simply:
Happy? It's going to be slower because you have nested function calls, it's going to be slower because it's going to iterate over every element instead of stopping.
But it uses jQuery.