1

I'm not even sure how to title this, so apologies up front.

I am using Underscore.js to do the following (create subtotals and spit out the updated array) this works as the values in the return are valid.

       var result = _.chain(data)
                      .groupBy("LDC_LoadPro")
                      .map(function (value, key) {
                          return {
                              LDC_LoadPro: value[0].LDC_LoadPro,
                              chr_amount: sum(_.pluck(value, "chr_amount")),
                              cmt_name :'Total'
                          }
                      })
                      .value();

        var copyData = data;

        copyData.push(result);

Next when a user wants to sort the data, I need to recalculate the subtotals based on the column they sort. Currently I'm passing the 'columnName' and all is working until I get to the underscore.js .map() and I want to use the columnName variable in the subtotal object. Is there some way to achieve this or should I rethink the process?

       var result = _.chain(notSubtotalRecords)
               .groupBy(columnName)
               .map(function (value, key) {
                   return {
                       columnName: value[0].columnName,
                       chr_amount: sum(_.pluck(value, "chr_amount")),
                       cmt_name: 'Total'
                   }
               })
               .value();
      console.log(result);

      notSubtotalRecords.push(result);

enter image description here

Brad Martin
  • 5,637
  • 4
  • 28
  • 44
  • 1
    It's hard to tell what you're trying to do, but you may be looking for `value[0][columnName]` to use the "columnName" variable's value as a property selector. – Pointy Apr 13 '15 at 15:27
  • Yea sorry for this being a disaster. What I'm doing is on the click event of the table header I pass a string containing the key of the array. Then I set that to var columnName and want to use it in the .map() – Brad Martin Apr 13 '15 at 15:29
  • Well there's nothing stopping you from using the variable in the `.map()` callback. What's not clear is what you need to do with it. What do those `value` components look like? Your code says `value[0]`; is it an array? What's in the array? – Pointy Apr 13 '15 at 15:31
  • I've added a picture to the post. In the highlighted line, you'll see the commented out line 'LDC_LoadPro: value[0].LDC_LoadPro. What I want is to use the var columnName (string) but it returns undefined in the result. – Brad Martin Apr 13 '15 at 15:33
  • Ah OK, I see what you mean. You can't do that :) You have to build the object and then add the property afterwards. (Let me look for a good duplicate question; this comes up a lot.) – Pointy Apr 13 '15 at 15:35
  • I've found several questions talking about keys/values and strings but nothing getting me on the right track. Appreciate the help. I really didn't want to hardcode this for each column :) – Brad Martin Apr 13 '15 at 15:37

0 Answers0