MattMcCode
MattMcCode

Reputation: 307

Reformat an array of objects to an array of key'd arrays

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,
         }]
   }]


 ]

Upvotes: 0

Views: 137

Answers (2)

CertainPerformance
CertainPerformance

Reputation: 371019

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);

Upvotes: 1

Martin Ad&#225;mek
Martin Ad&#225;mek

Reputation: 18399

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.

Upvotes: 1

Related Questions