如何以jQuery方式搜索和操作复杂的JavaScript对象 [英] How to Search and Manipulate Complex JavaScript Objects in a jQuery fashion

查看:72
本文介绍了如何以jQuery方式搜索和操作复杂的JavaScript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Web应用程序的上下文中,我有一个服务器,该服务器根据来自客户端的输入发送或接收JSON字符串.在客户使用时,这些JSON字符串会立即转换为JavaScript对象,在其中它们将以对象的形式存在.这些对象不是数组.它们表示复杂的任意数据模型,其每个属性可以具有任意数量的唯一子属性或对象.

In the context of a web app, I have a server which sends or receives JSON strings based on the input from the client. On client consumption, these JSON strings are immediately converted into JavaScript objects where they will live out their lives as objects. These objects are not arrays; they represent complex, arbitrary data models, each property of which can have an arbitrary number of unique subproperties or objects.

var myView = {
    name: 'root'
    id: 'root_0'
    children: {
       child_1: {
          arbitraryid: 'root_0_child_1',
          type: 'Department',
          name: 'Produce',
          quadrant: 1,
          children: {
              child_1: {
                  arbitraryid: 'root_0_child_1_child_1',
                  type: 'Aisle',
                  number: 3,
                  length: 12,
                  children: { }
              }
          }
       },
       child_2: {
          arbitraryid: 'root_0_child_2',
          name: 'West',
          type: 'Region',
          children: {
              child_1: {
                  arbitraryid: 'root_0_child_2_child_1',
                  name: 'Wegmans',
                  type: 'Store', 
                  children: { 
                      child_1: {
                          arbitraryid: 'root_0_child_2_child_1_child_1',
                          type: 'Department',
                          children: { }
                      }
                  }
             }
          }      
       }
    }
};

在构建JSON字符串服务器端时,我保证所有对象都将具有'children'和'arbitraryid'属性;但是其他所有内容都是动态生成的,并且属性和值完全是任意的.

When I build the JSON string server side, I guarantee that all objects will have 'children' and 'arbitraryid' properties; but everything else is dynamically generated and the properties and values are completely arbitrary.

如果这是XML,我可以使用jQuery来var someChild = myView.find('#root_0_child_1_child_1').这将使我得到一个jQuery对象,其结果为find,不仅对myView进行引用,而且对整个对象进行全方位移动,从位置 :var someChild = myView.find('#root_0_child_1_child_1').parent().

If this were XML, I could use jQuery to var someChild = myView.find('#root_0_child_1_child_1'). This would get me a jQuery object with the results of the find AND not only a reference to myView but a position from which to move omnidirectionally through the object: var someChild = myView.find('#root_0_child_1_child_1').parent().

是否存在实用程序来解决本机JavaScript对象的此问题,或者是否有更好的方法/方法来执行此操作?我想避免编写一堆这样的代码来简单地获取我的属性,然后可能再次循环以更新父对象.

Does a utility exist to solve this problem for native, JavaScript objects or is there a preferable way/methodology to do this? I'd like to avoid writing a bunch of this type of code to simply get at my property and then potentially loop again to update the parent object.

while (obj.hasOwnProperty('children')) {
   for (var child in obj) {
     //..etc, etc
   } 
}

我在该主题上看到的大多数SO问题都通过搜索数组处理/a>,经常使用可预测的数据表样式构建.

Most of the SO questions I see on this subject deal with searching arrays, frequently with predictable data table style construction.

可以进行映射,但是这些对象会很快变深,并且该选项似乎比哑循环更好.

Mapping is possible, but these objects quickly become deep and that option seems little better than dumb looping.

想法?

滚动我自己的实用程序类.

rolling my own utility class.

我仍在探索其他库/实用程序,但我编写了一个通用帮助程序类来进行搜索:

I'm still exploring other libraries/utilities, but I wrote a generic helper class to do searches:

ObjectHelper

虽然有用,但我认为它说明了获得其他类似jQuery的功能时遇到的一些困难.我不仅要搜索,而且希望能够像将.parent().children().find()运算符链接在一起的方式一样,上/下爬行对象属性结构.可行,但很复杂.

While useful, I think it illustrates some of the difficulty with getting at other jQuery-like functionality. Not only would I like to search, but I'd like to be able to crawl up/down the object property structure similarly to the way you can chain .parent().children().find() operators together. Doable, but complicated.

推荐答案

我通过自己滚动来解决了此问题类. ShadesJS目前非常基础,但是它具有一些用于爬网JavaScript对象和使用Web Storage的方法.待办事项是实现父/子方法以获得某种JQuery风格的灵活性.这样做并不难,但是要使其正确并具有高性能是很棘手的.

I solved this by rolling my own classes. ShadesJS is pretty basic right now, but it has some methods for crawling JavaScript objects and working with Web Storage. On the to-do is implement parent/child methods to get some JQuery-esque flexibility. It's not hard to do, but it's tricky to get it right and performant.

这篇关于如何以jQuery方式搜索和操作复杂的JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆