今天需要將一個對象push到一個對象數組中
const tableColumns = [ { title: '級別', dataIndex: 'VIP', width: 200 }, { title: '比例', dataIndex: 'FEE' }, ];
const operationColumns = { title: '操作', fixed: 'right', key: 'operation',width: 100, render(text, record, key) { return ( <span> <a href="javascript:void(0)" onClick={() => { obj.handlerDeleteBtnClick(text, record, key); }} >刪除</a> </span> ); }, }
將operationColumns 的對象push進去tableColumns
一開始的寫法
const columns = oprType !== 3 ? tableColumns.push(operationColumns) : tableColumns;
然后使用的columns,結果報錯,后來發現tableColumns.push(operationColumns)返回的是push之后數組的長度
后來進行了修改
oprType !== 3 ? tableColumns.push(operationColumns) : tableColumns;
使用數組tableColumns,這樣就沒問題了