Long XML - optimized sorting (js - google adwords)

57 views Asked by At

I'm working in a piece of code, and I think my brain is in an infinite loop.

I'm working with adwords scripts (pure JS).

I receive an XML Feed of products (more or less, there are 50000 products in the feed). Those products come with a lot of attributes (product type, brand, name, id, etc).

In Adwords, every account group its products in different levels (one account group them by 'Product Type > brand', other by 'brand > name', other by 'productType1 > brand > productType2 > Id', etc).

The logic is simple. When a group of products fulfill a condition (for example, it have more than 1000 clicks), I need to create one more level. For example, ProductType "Glasses" has a lot of products. When "Glasses" has more than 1000 clicks, I have to subdivide "Glasses" with its brands: "Glasses > RayBan", "Glasses > Gucci", etc. If any of those brands, have more than 1000 clicks, I subdivide again with its name.

As every account as its levels, I've created a levels array. For example:

var order = ['ProductTypeL1', 'Brand', 'OfferId'];

This order array will be different for every account.

At first, I saved all the products in a simple array:

for (var i = 0; i < products.resources.length; i++) {
  allProducts.push(products.resources[i]);
}

But later, when I get a report with product types, brands, names, etc, with more than 1000 clicks, I have to make a loop to check what products have the same, for example, productType than my new report.

I walk through the order array, asking if any group (of this level) fulfill the condition:

for(i=0; i<order.length-1;i++){
   //
   //logic
   //
}

In conclusion, I have multiple loops, that are not efficients.

To solve it, I thought that there must be another method to save allProducts in an associative array, where de indexName could be the productType Name, or the brandName, or whatelse, and later I only would have to ask if exists

allproducts[valueForThisReportProductType]

or

allproducts[valueForThisReportProductType][valueForThisReportBrand]

And here is where I am. I don't know how to create that type of array, or object, JSON, or mix.

The idea is that I need to be able to access the first level, and ask for the name of all its childs, and every child have its own childs.

Any ideas?

Sorry for my very bad english, I need to practice it much more.

1

There are 1 answers

0
user3592436 On

Finally, I declared a multidimensional array

if(order.length == 1){
    allProducts = [];
}else if(order.length == 2){
    allProducts = [][];
}else if(order.length == 3){
    allProducts = [][][];
}else if(order.length == 4){
    allProducts = [][][][];
}

And:

for (var i = 0; i < products.resources.length; i++) {

(...)

      var columna0 = columnasFeed(order[0], products.resources[i]);
      var columna1 = columnasFeed(order[1], products.resources[i]);
      var columna2 = columnasFeed(order[2], products.resources[i]);

      allProducts[columna0][columna1][columna2] = columna2;

(...)

}

Where columnasFeed() returns the value of that column of the Feed.

And now, I can check easily if an index is defined.

Now that I have it working, I only need to reduce the repeated code.