0

So I am kind of new to Javascript but I am trying to reformat the startArray into the endArray. The startArray is basically an array of order objects, which will always have different orderId's but can have the same companyId. I basically am trying to switch it so it's based on company, and so that for each companyId, there is an array of all of that companies orders. I have been tinkering and trying to figure this out, but to be honest I'm not sure where to start, or if this manipulation is even possible.

I am working out of Google Apps Scripts, which I think is still on ES5 syntax, and I would prefer to stick to vanilla javascript if at all possible.

var startArray = [
    {"companyId" : 1,
     "orderId" : 25,
     "product" : "productA"
     "quantity" : 2,
     "price" : 10,
    },
    {"companyId" : 1,
     "orderId" : 20,
     "product" : "productB"
     "quantity" : 3,
     "price" : 5,
    },
   {"companyId" : 2,
     "orderId" : 15,
     "product" : "productA"
     "quantity" : 5,
     "price" : 10,
    }
 ]

 var endArray = [ {
      '1' = [{"orderId" : 25,
         "product" : "productA"
         "quantity" : 2,
         "price" : 10,},
         {"orderId" : 20,
          "product" : "productB"
          "quantity" : 3,
          "price" : 5,}
      },{
     '2' = [{"orderId" : 15,
          "product" : "productA"
          "quantity" : 5,
          "price" : 10,
         }]
   }]


 ]
3
  • Are you sure you want the final product to be an array, and not an object? It would probably make more sense to just be an object indexed by companyId keys, rather than an array with a single object inside Commented Apr 27, 2018 at 18:40
  • I think that would work as well! Is that possible to do? Do you mean like 1 single object, and every key is a companyId? Commented Apr 27, 2018 at 18:41
  • As in, instead of the result being like your posted endArray, it would be like your endArray[0], since it looks like there'll only ever be one item in the array. (in which case why have an array at all) Commented Apr 27, 2018 at 18:49

2 Answers 2

1

Use something like reduce to create a new object, inserting an array as the key of the companyId if it doesn't exist already:

function reduce(arr, callback, initialVal) {
  var accumulator = (initialVal === undefined) ? undefined : initialVal;
  for (var i = 0; i < arr.length; i++) {
    if (accumulator !== undefined)
      accumulator = callback.call(undefined, accumulator, arr[i], i, this);
    else
      accumulator = arr[i];
  }
  return accumulator;
}

var startArray = [
  {"companyId" : 1,
   "orderId" : 25,
   "product" : "productA",
   "quantity" : 2,
   "price" : 10,
  },
  {"companyId" : 1,
   "orderId" : 20,
   "product" : "productB",
   "quantity" : 3,
   "price" : 5,
  },
  {"companyId" : 2,
   "orderId" : 15,
   "product" : "productA",
   "quantity" : 5,
   "price" : 10,
  }
];

var endObj = reduce(startArray, function (accum, item) {
  var companyId = item.companyId;
  if (!accum[companyId]) accum[companyId] = [];
  delete item.companyId;
  accum[companyId].push(item);
  return accum;
}, {});
console.log(endObj);

Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly, thank you so much @CertainPerformance!
1

If you are fine with result being object and not array, this should do the job:

var startArray = [
    {"companyId" : 1,
     "orderId" : 25,
     "product" : "productA",
     "quantity" : 2,
     "price" : 10,
    },
    {"companyId" : 1,
     "orderId" : 20,
     "product" : "productB",
     "quantity" : 3,
     "price" : 5,
    },
   {"companyId" : 2,
     "orderId" : 15,
     "product" : "productA",
     "quantity" : 5,
     "price" : 10,
    }
 ];

var ret = {}

startArray.forEach(company => {
  if (!ret[company.companyId]) {
    ret[company.companyId] = [];
  }
  
  ret[company.companyId].push(company);
});

console.log(ret);

If you do not have ES6 support, just use classic for in cycle instead of forEach.

1 Comment

Apps Script is JS 1.6 with some extras - one of which is Array#forEach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.