Skip to content
Advertisement

javascript filter elements not found in second array

solved: requires explicit return statement for each filter. I thought the single boolean in each filter would be clear enough. by @adiga

I want to find the elements in one array (dcm) that are not found in a second array (vari). I want to match only two elements, vp (string type) and vd (date type). I’ve made sure there are some rows in dcm that meet the condition, but I’m getting no results.

Did I do up the code wrong? is there a better way to do this (.includes .contains .indexOf)?

  var dcmm = dcm.filter(r=>{
    vari.filter(rv=>{
      rv[vp]+rv[vd] == r[dp]+r[dd]
      }).length == 0
    });

ps. sorrynotsorry to all the long variable name proponents out there. as well as the const-not-var proponents. pps. this is google apps script not javascript, but I think the idea is the same.

Advertisement

Answer

Just in case, as it said @adiga you don’t need return statements if you don’t use {}.

Most likely this will work fine:

var dcmm = dcm.filter( r => vari.filter( rv => (rv[vp]+rv[vd] == r[dp]+r[dd]) ).length == 0 );
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement