Cartesian product is the Cartesian product, also known as the direct product, of two sets X and Y, expressed as X × Y, where the first object is a member of X and the second object is a member of all possible ordered pairs of Y.

example

Assuming that set A = {A, b}, set b = {0, 1, 2}, the two sets of cartesian product for {(A, 0), (A, 1), (A, 2), (b, 0), (b, 1), (b, 2)}.

In the general implementation, C language, Python, Java implementation more ways, but for the front end, it also has its implementation significance,

  • For example, the realization of skU commodity order combination in Taobao requires Cartesian product, and n possible combinations are generated according to the sub-types and different sizes of commodities
  • In some cases to find missing data in consecutive dates, the Cartesian product can be used as a performable combination, and then associated with the target table to find what data is missing
  • SQL > select * from ‘MySQL’ where ‘MySQL’ = ‘multitable’
  • Generate chess coordinates

Wait, only you can’t imagine, without it can’t achieve. Let’s take a look at his concrete implementation!

Javascript implementation of cartesian product

/* * @Author: Mr Jiang.Xu * @Date: 2019-08-31 00:05:33 * @Last Modified by: Mr Jiang.Xu * @Last Modified time: The 2019-08-31 00:05:33 * /
function cartesian(arr) {
    if (arr.length < 2) return arr[0] | | [];return [].reduce.call(arr, function (col, set) {
        let res = [];
        col.forEach(c= > {
            set.forEach(s= > {
                let t = [].concat(Array.isArray(c) ? c : [c]); t.push(s); res.push(t); })});return res;
    });
}
Copy the code

Because of the implementation of many methods, here is not an example, the implementation of the time complexity of O(n^3), is not optimal, so there are better implementation methods welcome to leave a message to achieve oh ~

If you want to learn more JS algorithm and data structure, you can long press attention oh ~ due to the recent work changes, and prepare to develop a CMS open source system, so the article may try to update once a week, welcome to learn and progress together.

More recommended

  • JavaScript binary tree and binary search tree implementation and application
  • With JavaScript and C3 to achieve a turntable small game
  • Teach you to use 200 lines of code to write a love bean spell H5 small game (with source code)
  • Exploration and summary of front-end integrated solutions based on React/VUE ecology
  • How to use gulP4 development project scaffolding
  • How to write your own JS library in less than 200 lines of code
  • A summary of common JS functions that make you more productive in an instant
  • A picture shows you how to play vue-Cli3 quickly
  • 3 minutes teach you to use native JS implementation with progress listening file upload preview component
  • 3 minutes teach you to use native JS implementation with progress listening file upload preview component
  • Developing travel List with Angular8 and Baidu Maps API
  • Js basic search algorithm implementation and 1.7 million data under the performance test
  • How to make front-end code 60 times faster
  • Front End Algorithm series array deweighting
  • Vue Advanced Advanced series – Play with Vue and vuex in typescript
  • In the first three years, talk about the top five books worth reading