JSFiddle - React, Tailwind, and code Playground

by Pratik Galoria

JavaScript

const data = [{
  pB1: true,
  pB2: false,
  cp: 10,
}, {
  pS1: true,
  pS2: false,
  cp: 20
}, {
	pB1: true,
  pB2: true,
  cp: 30,
}, {
	pS1: true,
  pS2: true,
  cp: 40
}];

const combinations = [];
const buyingParams = ['pB1', 'pB2'];
const sellingParams = ['pS1', 'pS2'];

const getAllSubsets = theArray => theArray.reduce(
  (subsets, value) => subsets.concat(
    subsets.map(set => [value, ...set])
  ),
  [[]]
).filter(a => a.length);

const bc = getAllSubsets(buyingParams);
const sc = getAllSubsets(sellingParams);

const report = [];

for (let i = 0; i < bc.length; i++) {
	for (let j = 0; j < sc.length; j++) {
  	const reportEntity = { combination: bc[i].concat(sc[j]) };
    
    let isBought = false;
    let tp = 0;
    let q = 10;
    const profit = data.reduce((total, d) => {
    	const isBCmet = bc[i].reduce((met, c) => met && d[c], true);
      const isSCmet = sc[j].reduce((met, c) => met && d[c], true);
      
    	if (!isBought && isBCmet) {
      	isBought = true;
        tp = d['cp'] * q;
        return total;
      } else if (isBought && isSCmet) {
      	isBought = false;
        const sp = d['cp'] * 10;
        const pr = sp - tp;
        tp = 0;
        return total + pr;
      } else {
      	return total;
      }
    }, 0);
    
    Object.assign(reportEntity, { profit });
    report.push(reportEntity);
  }
}

console.log(report);