JSFiddle - React, Tailwind, and code Playground

by lasitha_prabodha

JavaScript

test_product_attributes = [];
this.temp = {
  color: [],
  size: {
    parent: '',
    child: []
  },
  material: []
};

_.forEach(this.available_fields, function(attr_value, key) {
  let loop_array = (attr_value.is_has_child !== null) ? this.temp[key].child : this.temp[key];

  if (loop_array.length != 0) {
    _.forEach(loop_array, function(temp_value, temp_key) {
      let obj = {
        is_available: true,
        qty: 0,
        price: 0,
      };

      obj.color = (attr_value.is_has_child !== null) ?
        _.find(attr_value.options[this.temp[key].parent], ['id', temp_value]) :
        _.find(attr_value.options, ['id', temp_value]);

      test_product_attributes.push(obj);

      for (let i in test_product_attributes) {
        if (!test_product_attributes[i][key]) {
          test_product_attributes[i][key] = (attr_value.is_has_child !== null) ?
            _.find(attr_value.options[this.temp[key].parent], ['id', temp_value]) :
            _.find(attr_value.options, ['id', temp_value]);
        }
      }

    }.bind(this));
  }
}.bind(this));