首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在.concat中优化es6方法

如何在.concat中优化es6方法
EN

Stack Overflow用户
提问于 2018-03-15 16:34:19
回答 2查看 46关注 0票数 0

如何使用.contact特性优化ES6方法。我的行很好用,但是我正在学习ES6,A想得到一些很好的例子来用一些es6特性来编写这段代码。(破坏或其他很酷的方式)谢谢

这一行需要优化,因为我不喜欢:

代码语言:javascript
复制
const list_B = buttonsType.toolz.concat(buttonsType.layerz,buttonsType.categoryz,buttonsType.sheetz,buttonsType.actionz);//***line to optimize */

具体情况如下:

代码语言:javascript
复制
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);
           };
        })();

编辑:已解决的

代码语言:javascript
复制
    (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;
       });
   };
})();
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-15 16:49:54

你可以用Object.entries

代码语言:javascript
复制
let a = [];
Object.entries(buttonsType).forEach(item => {a = a.concat(item[1])});
票数 1
EN

Stack Overflow用户

发布于 2018-03-15 16:42:36

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

代码语言:javascript
复制
const list_B = Object.keys(buttonsType)
  .reduce((acc, key) => {
    return acc.concat(buttonsType[key]);
  },[]); 

迭代buttonsType中的键。将按钮内的每个数组合并到空数组中-- the的键

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49304726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档