• <bdo id='MImO0'></bdo><ul id='MImO0'></ul>

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

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

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

      如何将值数组添加到 Set

      时间:2023-08-01

          <legend id='V760w'><style id='V760w'><dir id='V760w'><q id='V760w'></q></dir></style></legend>
            <bdo id='V760w'></bdo><ul id='V760w'></ul>

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

              • <tfoot id='V760w'></tfoot>
                <i id='V760w'><tr id='V760w'><dt id='V760w'><q id='V760w'><span id='V760w'><b id='V760w'><form id='V760w'><ins id='V760w'></ins><ul id='V760w'></ul><sub id='V760w'></sub></form><legend id='V760w'></legend><bdo id='V760w'><pre id='V760w'><center id='V760w'></center></pre></bdo></b><th id='V760w'></th></span></q></dt></tr></i><div id='V760w'><tfoot id='V760w'></tfoot><dl id='V760w'><fieldset id='V760w'></fieldset></dl></div>
                  <tbody id='V760w'></tbody>
              • 本文介绍了如何将值数组添加到 Set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                将数组的所有值添加到 Set 的老式方法是:

                The old school way of adding all values of an array into the Set is:

                // for the sake of this example imagine this set was created somewhere else 
                // and I cannot construct a new one out of an array
                let mySet = new Set()
                
                for(let item of array) {
                  mySet.add(item)
                }
                

                有没有更优雅的方式来做到这一点?也许是 mySet.add(array)mySet.add(...array)?

                Is there a more elegant way of doing this? Maybe mySet.add(array) or mySet.add(...array)?

                PS:我知道两者都不起作用

                PS: I know both do not work

                推荐答案

                虽然 Set API 仍然非常简约,但您可以使用 Array.prototype.forEach 并稍微缩短您的代码:

                While Set API is still very minimalistic, you can use Array.prototype.forEach and shorten your code a bit:

                array.forEach(item => mySet.add(item))
                
                // alternative, without anonymous arrow function
                array.forEach(mySet.add, mySet)
                

                这篇关于如何将值数组添加到 Set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:从数组中,生成所有不同的、非空的子数组,并保持顺序 下一篇:使用 ajax 请求设置引用 URL

                相关文章

                1. <tfoot id='4LtY6'></tfoot>
                2. <small id='4LtY6'></small><noframes id='4LtY6'>

                    <bdo id='4LtY6'></bdo><ul id='4LtY6'></ul>
                3. <legend id='4LtY6'><style id='4LtY6'><dir id='4LtY6'><q id='4LtY6'></q></dir></style></legend>

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