在对象上使用映射意外使用逗号

时间:2018-02-07 02:19:24

标签: javascript eslint airbnb

我使用AirBnb风格指南并意外使用逗号"第一行代码末尾的错误:

this.multiSelectorObj =  Ext.create('Ext.view.MultiSelector', {
            valueField: 'id',
            displayField: 'name',
            showDefaultSearch: true,
            plusButtonType: 'add',
            hideHeaders: true,
            colspan: 2,
            search: {
                xtype: 'multiselector-search',
                store: {
                    type: 'store',
                    fields: ['id', 'name'],
                    autoload: true,
                    data: [{ id: 1, name: 'Option 1 -- I want to change this' }] 
                }
            },
            showRemoveButton: true,
            columns: [{
                text: "Name",
                dataIndex: "name",
                flex: 1
            }]
        }
    );


Ext.create('Ext.form.Panel', {
    renderTo: Ext.getBody(),
    items: [
        {xtype: 'combobox',
            fieldLabel: 'Choose State',
            store: {
                fields: [ 'id', 'name' ],
                data: [
                    { id: 1, name: 'Set 1' },
                    { id: 2, name: 'Set 2' }
                ]
            },
            queryMode: 'local',
            displayField: 'name',
            valueField: 'id',
            listeners: {
                scope: this,
                'change': function(scope, newVal, oldVal, eOpts) {
                    //Attempting now to have a different set of options to choose from in the
                    //MultiSelector 
                    //But nothing seems to work
                    var search = this.multiSelectorObj.getSearch();
                    search.data = [{id: 1, name: 'Option Data Set 2'}]
                    this.multiSelectorObj.setSearch(search);

                //Nor does setting a new search store like this
                this.multiSelectorObj.getSearch().store = Ext.create('Ext.data.Store', {
                     data:[{id:1, name:'New Opt'}],
                     fields: ['id', 'name'],
                     autoLoad: true
                 });

            }
        },
    },

//MULTISELECTOR
this.multiSelectorObj
]

我可以将其重写为此以删除错误:

  myApp.fields.map(field => (field.obj.label = field.label.default,
    field.label.textContent = field.obj.label));

https://eslint.org/docs/rules/no-sequences

我看到第一段代码的方式,地图循环只运行一次第二个运行两次的字段。

我可以确认上面地图的两个部分都在执行,而不仅仅是最后一部分。还有其他我想念的东西吗?

1 个答案:

答案 0 :(得分:1)

由于您未对数组进行转换,因此您只能使用forEach()代替map()。如果你坚持要把它变成一个单行,并且不想违反无序列规则:

myApp.fields.forEach(field => {field.obj.label = field.label.default; field.label.textContent = field.obj.label});

更具可读性:

myApp.fields.forEach((field) => {
    field.obj.label = field.label.default;
    field.label.textContent = field.obj.label;
});
相关问题