Search code examples
javascriptecmascript-6ecmascript-5

Brackets inside function argument ES5 compatibility


I'm running the script below in IE11 with no transpilers. However, it returns an "expected identifier" error.

var result = Object.values(response.data.reduce(function(r, { boxm, model_no, model_name, qty, type }, index, array) {
      r[boxm] = r[boxm] || { boxm: boxm, lines: [] }
      r[boxm].lines.push({ model_no: model_no.toString(), model_name: model_name, qty: qty, type: type })
      return r
}, {}))

I'm guessing it must be this part { boxm, model_no, model_name, qty, type } in the reduce function.

Does someone know how do I convert this into ES5 friendly syntax.


Solution

  • The not supported part is for sure the deconstruction, but also the Object.values.

    To be sure about what you can use or not, there is a website:

    https://caniuse.com/#search=Object.values


    enter image description here


    var obj = response.data.reduce(function(r, data, index, array) {
      r[data.boxm] = r[data.boxm] || {
        boxm: data.boxm,
        lines: [],
      };
    
      r[data.boxm].lines.push({
        model_no: data.model_no.toString(),
        model_name: data.model_name,
        qty: data.qty,
        type: data.type,
      });
    
      return r;
    }, {});
    
    var result = Object.keys(obj).map(function(x) {
      return obj[x];
    });