0

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.

  • You stated what you need, but what is the actual question? It is expected that you show at least an attempt at solving this and some findings of your research on the issue. Could you provide us with those? – Emiel Zuurbier Jun 09 '22 at 07:21
  • 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. – user15754268 Jun 09 '22 at 07:29
  • 1
    You could use regular expressions to extract the values of `u` and `e` out of your combinations directly ... And then maybe split the `e` value into an array, then you can use "traditional" methods to look for a value inside an array for that part ... – CBroe Jun 09 '22 at 07:33

2 Answers2

0

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))
R4ncid
  • 6,944
  • 1
  • 4
  • 18
0

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);
SSM
  • 2,855
  • 1
  • 3
  • 10