Filter in the null returning array

Asked

Viewed 39 times

2

I have the following doubt, I am with that const

export const STATUS = [{
  0: 'Rascunho',
  1: 'Enviado',
  2: 'Processando',
  9: 'Processado',
  3: 'Agendado',
  4: 'Protocolizado',
  5: 'Suspenso',
  6: 'Erro protocolo',
  7: 'Erro processamento',
  8: 'Erro leitura',
}];

and I am trying to access the values using the following method

  public constantFormatter(params) {
    const status = STATUS.filter((p) => p === params.value);
  }

but it’s always returning empty and I’m not able to understand why, has anyone ever had the same problem ?

1 answer

3


This is because STATUS is an array of objects with within their status, and you are iterating on this array and not on the object values of it, if you know that their values exist within the first object of this array you can use the Object values. which will return an array from the values of the object passed. Example:

const STATUS = [{
  0: 'Rascunho',
  1: 'Enviado',
  2: 'Processando',
  9: 'Processado',
  3: 'Agendado',
  4: 'Protocolizado',
  5: 'Suspenso',
  6: 'Erro protocolo',
  7: 'Erro processamento',
  8: 'Erro leitura',
}];

function findValue(value){
  // Values agora é um array de Strings que contém todos os valores do objeto que está na posição 0 do array STATUS
  const values = Object.values(STATUS[0]);
  
  return values.find(v => v === value);
}

console.log(findValue('Enviado')); // Enviado

Before you go on asking other people questions about how to solve certain problems I advise you to first debug your code, do a console.log of its variables, see what is their value at a given time, make use of debbuger, this type of problem you can solve by simply looking at the value of the variables.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.