Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
Producto configurable mediante javascript/jquery

Producto Configurable

Hola,

Estoy tratando de generar productos configurables para los atributos seleccionados usando JavaScript. Quiero generar 3.0_Aluminium_1219, 3.0_Amber_1219, 3.8_Aluminium_1219 y 3.8_Amber_1219.

He usado el siguiente script para almacenar el valor de la opción en una matriz

 $('.complexSel').on('select2:select', function (e) { var obj = {} var keysArray = new Array(); var price = $('.complexSel').find(':selected'); $.each(price, function(index){ keysArray.push($(this).attr('data-mst-id')); if($(this).attr('data-mst-id') in obj ) { obj[$(this).attr('data-mst-id')].push({id:$(this).val(),text:$(this).text()}); }else{ obj[$(this).attr('data-mst-id')] = new Array(); obj[$(this).attr('data-mst-id')].push({id:$(this).val(),text:$(this).text()}); } }); var unique = keysArray.filter(function(itm, i, keysArray) { return i == keysArray.indexOf(itm); }); console.log(obj); }); 

ingrese la descripción de la imagen aquí

Por favor ayúdame a lograr el resultado.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

usó la siguiente función para resolver este problema.

 $('.complexSel').on('change', function (e) { var obj = {} //var obj = new Array(); var price = $('.complexSel').find(':selected'); $.each(price, function(index){ if($(this).attr('data-mst-val') in obj ) { obj[$(this).attr('data-mst-val')].push($(this).text()); }else{ obj[$(this).attr('data-mst-val')] = new Array(); obj[$(this).attr('data-mst-val')].push($(this).text()); } }); var cartesianRes = getCartesian(obj); console.log(cartesianRes); /* OUTPUT AS FOLLOWS*/ /* [ { "Size": "3.0", "Color": "Aluminum" }, { "Size": "3.0", "Color": "Amber" }, { "Size": "3.8", "Color": "Aluminum" }, { "Size": "3.8", "Color": "Amber" } ] */ }); function getCartesian(object) { return Object.entries(object).reduce((r, [k, v]) => { var temp = []; r.forEach(s => (Array.isArray(v) ? v : [v]).forEach(w => (w && typeof w === 'object' ? getCartesian(w) : [w]).forEach(x => temp.push(Object.assign({}, s, { [k]: x })) ) ) ); return temp; }, [{}]); }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda