如何使用.contact特性优化ES6方法。我的行很好用,但是我正在学习ES6,A想得到一些很好的例子来用一些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;
});
};
})();发布于 2018-03-15 16:49:54
你可以用Object.entries
let a = [];
Object.entries(buttonsType).forEach(item => {a = a.concat(item[1])});发布于 2018-03-15 16:42:36
我不知道是否可以谈论优化,但我会这样做:
const list_B = Object.keys(buttonsType)
.reduce((acc, key) => {
return acc.concat(buttonsType[key]);
},[]); 迭代buttonsType中的键。将按钮内的每个数组合并到空数组中-- the的键
https://stackoverflow.com/questions/49304726
复制相似问题