Currently, I am manipulating and reordering an array using data from a webpage. Initially, I reordered my array of objects based on a separate "sorting array" order. The original array looked like this:
[
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"eBay",
"providerName":"ebay",
"needProcessing":false
},
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"Cheap-Coilovers.co.uk",
"providerName":"pricerunner",
"needProcessing":false
},
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"House of Fraser",
"providerName":"connexity",
"needProcessing":false
},
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"notonthehighstreet.com",
"providerName":"connexity",
"needProcessing":false
}
]
I then utilized my sort function on this array:
function compare(a, b) {
let sortingArr = ['connexity', 'ecn', 'kelkoo', 'nexttag', 'pricerunner', 'shopping', 'ebay'];
if (sortingArr.indexOf(a.providerName) < sortingArr.indexOf(b.providerName)) {
return -1;
}
if (sortingArr.indexOf(a.providerName) > sortingArr.indexOf(b.providerName)) {
return 1;
}
return 0;
}
retailersOrdered.sort(compare);
This successfully orders the array of objects according to the sorting array, achieving the desired effect:
[
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"House of Fraser",
"providerName":"connexity",
"needProcessing":false
},
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"notonthehighstreet.com",
"providerName":"connexity",
"needProcessing":false
},
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"Cheap-Coilovers.co.uk",
"providerName":"pricerunner",
"needProcessing":false
},
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"eBay",
"providerName":"ebay",
"needProcessing":false
}
]
Now, my goal is to further refine this by ordering the array in a "round robin" fashion. This entails selecting the first object with providerName "connexity," followed by the first object with providerName "pricerunner," and then the first object with providerName "ebay," looping back to the start for subsequent iterations. The desired output would be:
[
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"House of Fraser",
"providerName":"connexity",
"needProcessing":false
},
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"Cheap-Coilovers.co.uk",
"providerName":"pricerunner",
"needProcessing":false
},
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"eBay",
"providerName":"ebay",
"needProcessing":false
},
{
"domain":"www.exampleurl.com/redirect",
"retailerName":"notonthehighstreet.com",
"providerName":"connexity",
"needProcessing":false
}
]
The challenge lies in dynamically building up this array with varying numbers of objects per provider. Any suggestions or knowledge of es2015 functionalities that could assist in accomplishing this dynamic sorting process would be greatly appreciated.