0

I tried to understand how using closure in my case that summarize data. Perhaps I should use map() or filter() before arrow function or instead of I don't know sure. I read MDN and this article Closures and arrow syntax but all useless. P.S. If my English is terrible - sorry for that. Many thanks for all your assistance.

data(){
            return{
                tableData: [
                    {
                        id: 1,
                        col1: 10,
                        col2: 20,
                    },
                    {
                        id: 1,
                        col1: 20,
                        col2: 30,
                    }
                ]
            }
        },
        computed: {
            getSum1(x) {
                    const findId = this.tableData.find(i => i.id === x);
                    console.log(findId);
                }
            },
<div class="table">
        <table>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
                <th>Summary</th>
            </tr>
            <tr>
                <td>{{tableData[0].col1}}</td>
                <td>{{tableData[0].col2}}</td>
                <td>{{getSum1}}</td>
            </tr>
            <tr>
                <td>{{tableData[1].col1}}</td>
                <td>{{tableData[1].col2}}</td>
                <td>{{getSum2}}</td>
            </tr>
        </table>
    </div>
  • You can get a sum of each item with `let sums = tableData.map(item => item.col1 + item.col2)`. This would be an array `[ 30, 50 ]`. If you have arbitrary number of columns it would be much easier to work with if they were in an array rather than individual keys. – Mark Oct 22 '18 at 21:30
  • sums comes array with two values and then I should separate it on two different values because i need insert data into different column. It's more difficult than I expect. Can you help me with closure? – Maxim Gordiyenko Oct 22 '18 at 22:40

0 Answers0