【问题标题】:How i can optimize .concat method in es6我如何在 es6 中优化 .concat 方法
【发布时间】:2018-08-24 13:48:30
【问题描述】:

如何使用 ES6 功能优化 .contact 方法。 我的线路工作良好,但正在学习 ES6 ,并且希望获得一些好的示例来编写具有某些 es6 功能的代码。 (解构或其他很酷的方式) 谢谢

因为我不喜欢而需要优化的行是:

const list_B = buttonsType.toolz.concat(buttonsType.layerz,buttonsType.categoryz,buttonsType.sheetz,buttonsType.actionz);//***line to optimize */

上下文是:

const buttonsType = {// slots Names for interactive buttons editor
            toolz:['iconParentMode', 'move', 'pivot', 'scaleicon', 'skewicon', 'rotateicon', 'drawLine', 'sunNigth', 'lockAll', 'playPause',],
            layerz:[ 'gb0', 'gb1', 'gb2', 'gb3', 'gb4', 'gb5', 'gb6',],
            categoryz:[ 'All', 'Characteres', 'Rocks', 'Trees', 'Buildings', 'Grass', 'FurnitureINT', 'FurnitureEXT', 'Cliffs', 'Objets', 'Divers copie',],
            sheetz:[ 'SpriteSheets', 'TileSheets', 'Spines', 'Sprites',],
            actionz:[ 'iconRenderable', 'icon_PinGrid', 'icon_grid', 'saveIcon',],
        };
    //////// ┌------------------------------------------------------------------------------┐
    //////// CREATE BUTTONS GRAFICS INTERACTIONS
    ////////└------------------------------------------------------------------------------┘
        // make and store buttons Data's
        (function(){
            // how i can optimise for es6 and write proper this concat ?
           const list_B = buttonsType.toolz.concat(buttonsType.layerz,buttonsType.categoryz,buttonsType.sheetz,buttonsType.actionz);//***line to optimize */

           for (let [i,len] = [0,list_B.length]; i < len; i++) {
               const name = list_B[i];
               const slot = $PME.gui.skeleton.findSlot(name);
           };
        })();

编辑:已解决

    (function(){
   const list_B = Object.entries(buttonsType);
   for (let [i,len] = [0,list_B.length]; i < len; i++) {
       const [type,list_name] = [list_B[i][0], list_B[i][1]];
       list_name.forEach(name => {
        buttonsSlots[name] = $PME.gui.skeleton.findSlot(name);
        buttonsSlots[name].type = type;
       });
   };
})();

【问题讨论】:

  • “优化”是什么意思?如果不想使用concat,可以使用两个for ... of ...(嵌套一个)。
  • 抱歉英语不是我的母语,意思是更易读,并使用 es6 功能进行优化。
  • 为什么首先在对象中有这些数组?键的含义是什么?
  • 正在用类别编辑器imgbox.com/tcgyYqer构建一个GUI

标签: javascript arrays ecmascript-6 concat destructuring


【解决方案1】:

我不知道是否有人可以谈论优化,但我会这样做:

const list_B = Object.keys(buttonsType)
  .reduce((acc, key) => {
    return acc.concat(buttonsType[key]);
  },[]); 

遍历buttonsType 中的键。连接到 buttonType 键中的 reduce 每个数组的空数组中

【讨论】:

  • 请注意,这并不能保证正确的连接顺序。
  • 它可以有来自buttonsType 的任意顺序的数组。
  • 是的订单不重要,感谢这段代码看起来不错
【解决方案2】:

你可以使用Object.entries

let a = [];
Object.entries(buttonsType).forEach(item => {a = a.concat(item[1])});

【讨论】:

  • 请注意,这是 ECMAScript 2017 (ES8) 的特性,而不是 ES6
  • 这段代码绝对写得超级好,可读性强。非常感谢它运行良好。
  •  (function(){ const list_B = Object.entries(buttonsType); for (let [i,len] = [0,list_B.length]; i  { buttonsSlots[name] = $PME.gui.skeleton.findSlot(name); buttonSlots[name].type = type; }); }; })(); 
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 2021-11-23
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
相关资源
最近更新 更多