title: SUM IFS in JavaScript
body: <p>How can I implement a Summation logic based on agreement of multiple true conditions, that is:</p>
  <p>I have an input array:</p>
  <p>value:    <code>[['dec','toy',12/4/2020,50],['dec','pen',14/4/2020,50],['dec','toy',14/4/2020,53],['dec','toy',4/4/2020,50],]</code></p>
  <p>header: <code>['month','object','day','score']</code></p>
  <p>Now I have a function that takes 4 parameters:</p>
  <ol>
  <li>header array</li>
  <li>value array</li>
  <li>condition string</li>
  </ol>
  <p><code>function sumif(summationMetricHeader,header,value,condition)</code></p>
  <p>Where <code>condition</code> string is as follows:
  <code>&lt;value to be compared&gt;@@&lt;comparision operation&gt;@@&lt;ValueType&gt;@@HeaderName,&lt;value to be compared&gt;@@&lt;comparision operation&gt;@@&lt;ValueType&gt;@@HeaderName...</code></p>
  <p>where @@ is the seperator specifying the 4 comparision params and , is the seperator between multiple conditions.</p>
  <p>If all the condition is true then <code>summationHeaderMetric</code> should be added upon.</p>
  <p>So for example:</p>
  <p>value:    <code>[['dec','toy',12/4/2020,50],['dec','pen',14/4/2020,50],['dec','toy',14/4/2020,53],['dec','toy',4/4/2020,50],]</code></p>
  <p>header: <code>['month','object','day','score']</code></p>
  <p>summationMetric: <code>score</code></p>
  <p>condition string: <code>12/4/2020@@&gt;=@@Date@@day,50@@&gt;=@@Number@@score</code></p>
  <p>Now here it says sum only if day column is greater than equal to <code>12/4/2020</code> (and comparision must be date comparision as it's specified) and also where score is greater than equal to 50 (here comparision is number type)</p>
  <p>So the output will be: 50 + 50 + 53 (as for the last element in values  array day column value is smaller than the specified condition)</p>
  <p>In total there are threee comparisions:</p>
  <ol>
  <li>Date</li>
  <li>String</li>
  <li>Number</li>
  </ol>
  <p>for comparisions:</p>
  <pre><code>let decision = true;
  if(stringElem.split(&quot;@@&quot;)[2] === 'Date')
          decision = decision &amp;&amp; eval(`${new Date(stringElem.split(&quot;@@&quot;)[0]).getTime()}${stringElem.split(&quot;@@&quot;)[1]}${new Date(value[i]).getTime()}`)
  
  
  else if(stringElem.split(&quot;@@&quot;)[2] === 'String')
          decision = decision &amp;&amp; eval(`'${stringElem.split(&quot;@@&quot;)[0]}'${stringElem.split(&quot;@@)[1]}'${value[i]}'`)
  </code></pre>
  <p>I tried of using eval but am not able to format the string properly for comparision. Can anyone help me to solve this? I am not very deep into Javascript array methods and would love to know if there's any simpler way to code the same.</p>
  <p>Here's a very simple version of the function I am trying to implement:</p>
  <pre><code>    function computeMetricwithConditions() {
  
    const headers = ['name', 'place','stats', 'score', 'date'];
    const data = [['o','b', 22, 2, '4/14/2021']];
    const conditions = [['o', '===','String','name'],
                        ['b', '===','String','place'],
                        ['4/14/2021','&gt;=','Date','date'],
                        [2,'&gt;=','Number','stats']];
    const hashMap = {};
  
    headers.forEach((element,index)=&gt; {
      hashMap[element] = index;
    });
  
    console.log(hashMap);
  
    data.forEach(element =&gt; {
  
      let dec = true;
  
      conditions.forEach(elem =&gt; {
      let comparisionValue = elem[0];
      let comparisionOperator = elem[1];
      let comparisionType = elem[2];
      let comparisionHeader = elem[3];
  
      if(comparisionType === 'String')
      {dec = dec &amp;&amp; eval(`'${comparisionValue}'${comparisionOperator}'${element[hashMap[comparisionHeader]]}'`);
      //console.log(`'${comparisionValue}'${comparisionOperator}'${element[hashMap[comparisionHeader]]}'`);
      }
      else if(comparisionType === 'Date')
      {
        dec = dec &amp;&amp; eval(`${new Date(comparisionValue).getTime()}${comparisionOperator}${new Date(element[hashMap[comparisionHeader]]).getTime()}`);
  
      }
  
      else
      {
        dec = dec &amp;&amp; eval(`${comparisionValue}${comparisionOperator}${element[hashMap[comparisionHeader]]}`);
      }
  
      console.log(dec);
    });
  
      });
  
      
  
  }
  </code></pre>
  <p>This is my implementation, not sure if its a hacky way of getting things done or there could be any better way for the same?</p>
  <p>for the date comparision. Not sure why that happens.</p>