1

I run the process below to gather all product attributes and options from an object, in order to update a product.

For some strange reason, the main array categoryUrls get changed, and I can't seem to understand why.

After the following process, categoryUrls[random key].attributes.options gets new values added.

const {categoryUrls} = require('./helpers/categoryUrls.js')

getCategory = function (categoryUrls) {

    let att = []
    
    for (const key in categoryUrls) {
        if (categoryUrls[key].attributes) {// check if has attribute 
            att = att.concat(categoryUrls[key].attributes)
            
            for(var i=0; i<att.length; ++i) {// check if attribute ID already exist and remove duplicates
                for(var j=i+1; j<att.length; ++j) {
                    if(att[i].id === att[j].id){
                        for (let k = 0; k < att[j].options.length; k++) { // if attribute ID already exist unite options
                            let exist = att[i].options.find(option => option == att[j].options[k]) // check if option no already exist 
                            if (!exist) {
                                att[i].options.push(att[j].options[k])
                            }
                        }
                        att.splice(j--, 1);
                    }
                }
            }
        }
    }
    return att
}


let data = getCategory(categoryUrls)
productData = productData.concat(data)
yoni
  • 129
  • 9
  • Looks like filter and reduce would shorten this code immensely - why do you reuse the att name? Makes it quite hard to follow – mplungjan Sep 05 '20 at 18:21

0 Answers0