<bdo id='cP123'></bdo><ul id='cP123'></ul>
<tfoot id='cP123'></tfoot>

    <small id='cP123'></small><noframes id='cP123'>

  1. <legend id='cP123'><style id='cP123'><dir id='cP123'><q id='cP123'></q></dir></style></legend>
    <i id='cP123'><tr id='cP123'><dt id='cP123'><q id='cP123'><span id='cP123'><b id='cP123'><form id='cP123'><ins id='cP123'></ins><ul id='cP123'></ul><sub id='cP123'></sub></form><legend id='cP123'></legend><bdo id='cP123'><pre id='cP123'><center id='cP123'></center></pre></bdo></b><th id='cP123'></th></span></q></dt></tr></i><div id='cP123'><tfoot id='cP123'></tfoot><dl id='cP123'><fieldset id='cP123'></fieldset></dl></div>

    1. 在 JavaScript 中压缩对象层次结构

      compressing object hierarchies in JavaScript(在 JavaScript 中压缩对象层次结构)

            <tbody id='WhpPl'></tbody>
          <i id='WhpPl'><tr id='WhpPl'><dt id='WhpPl'><q id='WhpPl'><span id='WhpPl'><b id='WhpPl'><form id='WhpPl'><ins id='WhpPl'></ins><ul id='WhpPl'></ul><sub id='WhpPl'></sub></form><legend id='WhpPl'></legend><bdo id='WhpPl'><pre id='WhpPl'><center id='WhpPl'></center></pre></bdo></b><th id='WhpPl'></th></span></q></dt></tr></i><div id='WhpPl'><tfoot id='WhpPl'></tfoot><dl id='WhpPl'><fieldset id='WhpPl'></fieldset></dl></div>
          <legend id='WhpPl'><style id='WhpPl'><dir id='WhpPl'><q id='WhpPl'></q></dir></style></legend>
            • <small id='WhpPl'></small><noframes id='WhpPl'>

              <tfoot id='WhpPl'></tfoot>

              • <bdo id='WhpPl'></bdo><ul id='WhpPl'></ul>
                本文介绍了在 JavaScript 中压缩对象层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                是否有一种通用方法可以将嵌套对象压缩"到单个级别:

                Is there a generic approach to "compressing" nested objects to a single level:

                var myObj = {
                    a: "hello",
                    b: {
                        c: "world"
                    }
                }
                
                compress(myObj) == {
                    a: "hello",
                    b_c: "world"
                }
                

                我想这会涉及到一些递归,但我认为我不需要在这里重新发明轮子......!?

                I guess there would be some recursion involved, but I figured I don't need to reinvent the wheel here... !?

                推荐答案

                function flatten(obj, includePrototype, into, prefix) {
                    into = into || {};
                    prefix = prefix || "";
                
                    for (var k in obj) {
                        if (includePrototype || obj.hasOwnProperty(k)) {
                            var prop = obj[k];
                            if (prop && typeof prop === "object" &&
                                !(prop instanceof Date || prop instanceof RegExp)) {
                                flatten(prop, includePrototype, into, prefix + k + "_");
                            }
                            else {
                                into[prefix + k] = prop;
                            }
                        }
                    }
                
                    return into;
                }
                

                您可以通过将 true 传递给第二个参数来包含继承成员.

                You can include members inherited members by passing true into the second parameter.

                一些注意事项:

                • 递归对象不起作用.例如:

                • recursive objects will not work. For example:

                var o = { a: "foo" };
                o.b = o;
                flatten(o);
                

                会递归直到抛出异常.

                就像 ruquay 的回答一样,这会像普通对象属性一样提取数组元素.如果要保持数组完整,请将|| prop instanceof Array"添加到异常中.

                Like ruquay's answer, this pulls out array elements just like normal object properties. If you want to keep arrays intact, add "|| prop instanceof Array" to the exceptions.

                如果您从不同的窗口或框架对对象调用此方法,日期和正则表达式将不包括在内,因为 instanceof 将无法正常工作.您可以通过将其替换为默认的 toString 方法来解决此问题,如下所示:

                If you call this on objects from a different window or frame, dates and regular expressions will not be included, since instanceof will not work properly. You can fix that by replacing it with the default toString method like this:

                Object.prototype.toString.call(prop) === "[object Date]"
                Object.prototype.toString.call(prop) === "[object RegExp]"
                Object.prototype.toString.call(prop) === "[object Array]"
                

                这篇关于在 JavaScript 中压缩对象层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)
                getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  <bdo id='qiYTC'></bdo><ul id='qiYTC'></ul>

                  1. <legend id='qiYTC'><style id='qiYTC'><dir id='qiYTC'><q id='qiYTC'></q></dir></style></legend>
                  2. <tfoot id='qiYTC'></tfoot>

                  3. <small id='qiYTC'></small><noframes id='qiYTC'>

                      <tbody id='qiYTC'></tbody>

                      <i id='qiYTC'><tr id='qiYTC'><dt id='qiYTC'><q id='qiYTC'><span id='qiYTC'><b id='qiYTC'><form id='qiYTC'><ins id='qiYTC'></ins><ul id='qiYTC'></ul><sub id='qiYTC'></sub></form><legend id='qiYTC'></legend><bdo id='qiYTC'><pre id='qiYTC'><center id='qiYTC'></center></pre></bdo></b><th id='qiYTC'></th></span></q></dt></tr></i><div id='qiYTC'><tfoot id='qiYTC'></tfoot><dl id='qiYTC'><fieldset id='qiYTC'></fieldset></dl></div>