Home > Net >  Why my arrow function with object filter does not work
Why my arrow function with object filter does not work

Time:09-02

Can anybody please tell me why my arrow function that filters an array of object does not work. The snippet is shortened but the idea is to combine two key values from one object and match it with another key value from another object.

 const fNFS_S = [{"Seq":0,"KeyReg":"020620222022000258","TaxId":"06.865.436/0001-20","KeyId":"86520","NfDate":"02/06/2022","RegDate":"02/07/2022","NfNum":"20220002","NfMod":"2022","NfVal":"200,00","IRRF":"0,00","RetISS":"0,00","RetPIS":"0,00","RetCOF":"0,00","RetCSLL":"0,00","NumFat":"2022000258","PgFat":"1"}];

 const fData = [{"docnum":"2022000258","vendor":"4.1.1.01.0003","docdate":"02/06/2022","expirydate":"02/07/2022","paymentcondition":"","category":"1.1.2.04.0001","itemdescrip":"CV:Valor ref.a servicos de mao de obra de industrializ. de moveis cfe nf nº 2022000258","itemvalue":"200,00","message":""}];

 var cnpjcheck = fData[0]['docdate'].replaceAll('/','')   fData[0]['docnum'];

 var keyregcheck = fNFS_S[0]['KeyReg'];

 var dupl = fNFS_S.filter(o => o.KeyReg === cnpjcheck); //this never get matched

I have even tried to debug their code units but it seem that they are really the same

 console.log(cnpjcheck === keyregcheck);

 for (var i = 0; i < cnpjcheck.length; i  ) {
            console.log(cnpjcheck.codePointAt(i), keyregcheck.codePointAt(i));
          }  

const fNFS_S = [{"Seq":0,"KeyReg":"020620222022000258","TaxId":"06.865.436/0001-20","KeyId":"86520","NfDate":"02/06/2022","RegDate":"02/07/2022","NfNum":"20220002","NfMod":"2022","NfVal":"200,00","IRRF":"0,00","RetISS":"0,00","RetPIS":"0,00","RetCOF":"0,00","RetCSLL":"0,00","NumFat":"2022000258","PgFat":"1"}];

 const fData = [{"docnum":"2022000258","vendor":"4.1.1.01.0003","docdate":"02/06/2022","expirydate":"02/07/2022","paymentcondition":"","category":"1.1.2.04.0001","itemdescrip":"CV:Valor ref.a servicos de mao de obra de industrializ. de moveis cfe nf nº 2022000258","itemvalue":"200,00","message":""}];

var cnpjcheck = fData[0]['docdate'].replaceAll('/','')   fData[0]['docnum'];

var keyregcheck = fNFS_S[0]['KeyReg'];

console.log(cnpjcheck === keyregcheck);

for (var i = 0; i < cnpjcheck.length; i  ) {
                console.log(cnpjcheck.codePointAt(i), keyregcheck.codePointAt(i));
              }  

var dupl = fNFS_S.filter(o => o.KeyReg === cnpjcheck);
console.log(dupl);

Edit: It does work here but does NOT work in my Google AppScript snippet

If you see my debugger the last value is the combined one trying to be matched with the second (KeyReg). One thing that caught my attention is that it's " has gone to another line. I have tried to trim() and normalize() it but nothing changed.

The result is it gets past and goes into a loop as it does not find it

Debugger Watch

CodePudding user response:

This is specified because its fNFS_S variable is an object not an array, so it doesn't have a filter method. try to declare it like this:

fNFS_S = [{"KeyReg":"020620222022000258"}];

CodePudding user response:

'fNFS_S' must be an object(s) inside an array. like this: const fNFS_S = [{"KeyReg":"020620222022000258"}];

  • Related