<tfoot id='yK23K'></tfoot>
  • <legend id='yK23K'><style id='yK23K'><dir id='yK23K'><q id='yK23K'></q></dir></style></legend>

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

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

        • <bdo id='yK23K'></bdo><ul id='yK23K'></ul>
      1. 如何在 Javascript 中对对象进行切片?

        时间:2023-10-21

          <tfoot id='7DD6S'></tfoot>
          • <bdo id='7DD6S'></bdo><ul id='7DD6S'></ul>

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

                <small id='7DD6S'></small><noframes id='7DD6S'>

                <legend id='7DD6S'><style id='7DD6S'><dir id='7DD6S'><q id='7DD6S'></q></dir></style></legend>
                  <tbody id='7DD6S'></tbody>
                • 本文介绍了如何在 Javascript 中对对象进行切片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图使用 Array.prototype 对对象进行切片,但它返回一个空数组,除了传递参数之外还有什么方法可以切片对象,还是只是我的代码有问题?谢谢!!

                  I was trying to slice an object using Array.prototype, but it returns an empty array, is there any method to slice objects besides passing arguments or is just my code that has something wrong? Thx!!

                  var my_object = {
                   0: 'zero',
                   1: 'one',
                   2: 'two',
                   3: 'three',
                   4: 'four'
                  };
                  
                  var sliced = Array.prototype.slice.call(my_object, 4);
                  console.log(sliced);
                  

                  推荐答案

                  我试图使用 Array.prototype 对对象进行切片,但它返回一个空数组

                  I was trying to slice an object using Array.prototype, but it returns an empty array

                  那是因为它没有 .length 属性.它将尝试访问它,获取 undefined,将其转换为数字,获取 0,并从对象中切出最多那么多属性.为了达到预期的结果,您必须为它分配一个 length,或者手动通过对象的迭代器:

                  That's because it doesn't have a .length property. It will try to access it, get undefined, cast it to a number, get 0, and slice at most that many properties out of the object. To achieve the desired result, you therefore have to assign it a length, or iterator through the object manually:

                  var my_object = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four'};
                  
                  my_object.length = 5;
                  console.log(Array.prototype.slice.call(my_object, 4));
                  
                  var sliced = [];
                  for (var i=0; i<4; i++)
                      sliced[i] = my_object[i];
                  console.log(sliced);

                  这篇关于如何在 Javascript 中对对象进行切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:JavaScript 函数中的默认参数值 下一篇:Javascript:传递具有匹配项的函数以替换(正则表达式,func(arg))不起作用

                  相关文章

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

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

                      <tfoot id='yUwRh'></tfoot>