Home > Software design >  Comparison based on valid string combination in javascript
Comparison based on valid string combination in javascript

Time:06-10

I have selected values on screen and their value are stored in two variables. var uval = '100'; var eval = '5';

There are 2 combinations that have values: let combination1= 'u:100;e:1,4,5,10' let combination2 = 'u:1000;e:120,400,500,1000'

I want to check if uval and eval are present in any combination and set some Boolean to true otherwise it'll be false. uval will be compared to u in that combination and eval with e in that combination.

Boolean in this example will be true. If uval='50'; eval='5' then it'll be false.

I tried using a for loop using split(';') and compare each u with uval and e with eval. I am looking for some different approach for this apart from using traditional for loops everytime.

CodePudding user response:

you can do something like this

basically I created a function that transform you combination into an object and I used that object inside the check function

const combination1= 'u:100;e:1,4,5,10' 
const combination2 = 'u:1000;e:120,400,500,1000'

const uval = '100'; 
const eval = '5';

const toObject = combination => Object.fromEntries(
   combination.split(';').map(c => c.split(':')).map(([k, v]) => [k, v.split(',')])
   )
   

const check = (combination, key , value ) => (toObject(combination)[key] || []).includes(value)

const checkEval = (combination, eval) => check(combination, 'e', eval)
const checkUval = (combination, uval) => check(combination, 'u', uval)

console.log(checkEval(combination1, eval))
console.log(checkEval(combination2, eval))
console.log(checkUval(combination1, uval))
console.log(checkUval(combination2, uval))

CodePudding user response:

I've created a function named getKeyValuePairs that returns key value pairs for a given combination.

So, if we call getKeyValuePairs with the combination "u:100;e:1,4,5,10" it would return the following array of key value pairs:

[
  [ 'u', [ '100' ] ],
  [ 'e', [ '1', '4', '5', '10' ] ]
]

Then I've created another function named groupCombinations that merges all the combinations passed to it into a single object.

So, if we call groupCombinations with combinations "u:100;e:1,4,5,10" and "u:1000;e:120,400,500,1000", it would return the following object:

{
  u: Set(2) {"100", "1000"},
  e: Set(8) {"1", "4", "5", "10", "120", "400", "500", "1000"}
}

Finally I've grouped all the combinations together using groupCombinations and then checked if the group contains uVal and eVal.

const getKeyValuePairs = (combination) =>
  combination
    .split(";")
    .map((str) => str.split(":"))
    .map(([k, v]) => [k, v.split(",")]);

const groupCombinations = (...combinations) =>
  Object.fromEntries(
    Object.entries(
      combinations.reduce(
        (group, combination) => (
          getKeyValuePairs(combination).forEach(([k, v]) =>
            (group[k] ??= []).push(...v)
          ),
          group
        ),
        {}
      )
    ).map(([k, v]) => [k, new Set(v)])
  );

const uVal = "100";
const eVal = "5";
const combination1 = "u:100;e:1,4,5,10";
const combination2 = "u:1000;e:120,400,500,1000";

const allCombinations = groupCombinations(combination1, combination2);
const isUVal = allCombinations["u"].has(uVal);
const isEVal = allCombinations["e"].has(eVal);
const isBoth = isUVal && isEVal;

console.log(isBoth);

  • Related