The _javascript technique of product release of Cartesian product based on JS

Source: Internet
Author: User
Tags extend

Before you introduce the text to everyone to add some knowledge:

JS Cartesian product algorithm

To generate a Cartesian product from a given object or array

Cartesian product combination
function Descartes (list)
{
//parent upper index; count pointer Count
var point = {};
var result = [];
var pindex = null;
var tempcount = 0;
var temp = [];
Generates a pointer object for a parameter column for
(var index in list)
{
if (typeof list[index] = = ' object ')
{
Point[index] = {' Parent ':p index, ' count ': 0}
pindex = index;
}
}
The single dimensional data structure directly returns
if (Pindex = = null)
{return
list;
}
Dynamically generating Cartesian product while
(true)
{for
(var index in list)
{
Tempcount = point[index][' count '];
Temp.push (List[index][tempcount]);
Press into the result array
result.push (temp);
temp = [];
Check the pointer maximum problem while
(true)
{
if (point[index][' count ']+1 >= list[index].length)
{
point[ index][' count '] = 0;
Pindex = point[index][' parent '];
if (Pindex = = null) {return result
;
}
Assign parent to check again
index = pindex;
}
else
{
point[index][' count ']++;
Break;}}}

Well, about the JS Cartesian product algorithm is just for the following to do a foreshadowing, not much to say, to get to the ground.

I. Description of requirements

The product launch function of the electric Dealer website, similar to Jingdong Commodity Detail page, the following figure, such optional function, in the background is how to generate it, in fact, you see a iphone6 in the release is not just a product, but very many, because each choice comes out of the iphone6 price is not the same, Then when the product is released, the optional items are selected from a heap of attributes and attribute values, the problem is that the number of attributes selected at the time of publication is not the same, the value of the property is different, then the number of products generated is based on attributes and attribute values combined.

Second, directly on the code

<script>/** * Commodity attribute type * A number of attributes is indeterminate */var Spec = function (specname,specitems) {this.specname = Specname;//property name th Is.specitems = specitems;//numeric value, is the array, the number of arrays is indeterminate} var result = [];//combined into a product set/** * Publish a property of a product selection, this is an array of specifications, the number of arrays is not determined * Based on this selected attribute combination Different products/var selectspec = [{specname: ' capacity ', specitems:[' 16G ', ' 64G ', ' 128G ']}, {specname: ' Color ', specitems:[' tyrants gold ', ' silver ', ' black '
, ' Pink ']}, {specname: ' network ', specitems:[' unicom ', ' move ', ' Telecom ']}; function combine (index, current) {if (Index < selectspec.length-1) {var specitem = Selectspec[index]; var Keya = Spec
Item.specname;
var items = Specitem.specitems; if (items.length==0) {Run (index + 1, current);} for (var i = 0; i < items.length i++) {if (!items[i)) continue; var new
Map = {};
Newmap = $.extend (newmap,current);
Newmap[keya] = items[i];
Run (index + 1, newmap); }}else if (index = = selectspec.length-1) {var specitem = Selectspec[index]; var keya = Specitem.specname; var items = s
Pecitem.specitems; if (items.length==0) {Result.push (current);} for (var i = 0; i < items.length; i++) {if (!items[i]) continue; var newmap = {}; newmap = $.extend (newmap,current); Newmap[keya] = items[i]; Result.push (NEWM
AP);
}} combine (0, {});
Console.info (result); /** combined into product set * [Object {capacity = "16G", color = "Tyrants Gold", network = "Unicom"}, * Object {capacity = "16G", color = "Tyrants Gold", network = "Move"}, * Object {capacity = "16G", color = "tyrants" Gold ", network =" Telecommunication "}, * Object {capacity =" 16G ", color =" Silver ", network =" Unicom "}, * Object {capacity =" 16G ", color =" Silver ", network =" Move "}, * Object {capacity =" 16G ", color =" Silver " ", network =" Telecom "}, * Object {capacity =" 16G ", color =" BLACK ", network =" Unicom "}, * Object {capacity =" 16G ", color =" BLACK ", network =" Move "}, * Object {capacity =" 16G ", color =" Black Color ", network =" Telecom "}, * Object {capacity =" 16G ", color =" Pink ", network =" Unicom "}, * Object {capacity =" 16G ", color =" Pink ", network =" Move "}, * Object {capacity =" 16G ", color = "Pink", network = "Telecom"}, * Object {capacity = "64G", color = "Tyrants Gold", network = "Unicom"}, * Object {capacity = "64G", color = "Tyrants Gold", network = "Move"}, * Object {capacity = 6 4G ", color =" Tyrants Gold ", network =" Telecom "}, * Object {capacity =" 64G ", color =" Silver ", network =" Unicom "}, * Object {capacity =" 64G ", color =" Silver ", network =" Move "}, * Object {capacity = 6 4G ", color =" Silver ", network =" Telecom "}, * Object {capacity =" 64G ", color =" BLACK ", network =" Unicom "}, * ObjeCT {capacity = "64G", color = "BLACK", network = "Move"}, * Object {capacity = "64G", color = "BLACK", network = "Telecom"}, * Object {capacity = "64G", color = "Pink", network = "Unicom"}, * O bject {capacity = "64G", color = "Pink", network = "Move"}, * Object {capacity = "64G", color = "Pink", network = "Telecom"}, * Object {capacity = "128G", color = "Tyrants Gold", network = "Unicom" }, * Object {capacity = "128G", color = "Tyrants Gold", network = "Move"}, * Object {capacity = "128G", color = "Tyrants Gold", network = "Telecom"}, * Object {capacity = "128G", color = "Silver", net Contact = "Unicom"}, * Object {capacity = "128G", color = "Silver", network = "Move"}, * Object {capacity = "128G", color = "Silver", network = "Telecom"}, * Object {capacity = "128G", color = "BLACK" ", network =" Unicom "}, * Object {capacity =" 128G ", color =" BLACK ", network =" Move "}, * Object {capacity =" 128G ", color =" BLACK ", network =" Telecom "}, * Object {capacity =" 128G ", color = "Pink", network = "Unicom"}, * Object {capacity = "128G", color = "Pink", network = "Move"}, * Object {capacity = "128G", color = "Pink", network = "Telecom"}] * * * </script&  Gt

The above is a small set to introduce the Cartesian products based on the product of the product released to the content, I hope to help you, but also very grateful to the cloud Habitat Community website support, I believe we will do better!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.