I have the array of objects:
[ { pair_id: 1, exchange_pair_id: 183 }, { pair_id: 1, exchange_pair_id: 2}, ... ] I want to rebuild this array as
[ { pair_id: 1, exchange_pair_id: [183, 2] }, ... ] Here is the code I have written (the code which brings me closest to the desired result anyway):
var array = []; rows.forEach(function(row) { var obj = {}; obj.pair_id = [row.pair_id]; obj.exchange_pair_id = [row.exchange_pair_id] array.push(obj); }); Which results in:
[ { pair_id: 1, exchange_pair_id: [183] }, { pair_id: 1, exchange_pair_id: [2] }, ... ] This seems like a very simple problem with a simple solution, but I've been wracking my brains and can't figured it out.
obj.exchange_pair_id = [row.exchange_pair_id]is wrap the value into an array but not check for similar pair ids. The simplest idea would be using keys in your array.