TransWikia.com

filter method returns empty array

Stack Overflow Asked on November 15, 2021

I have the following doubt, I have this 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’m trying to access the values ​​using the following method

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

however it is always returning empty and I am not able to understand why, has anyone had the same problem?

3 Answers

Assuming you cannot edit the original array, you can get object values from it using Object.values().

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'
}];

console.log(Object.values(STATUS[0]));

Answered by tripathiakshit on November 15, 2021

What you most likely want is an enum:

export enum STATUS {
  Rascunho,
  Enviado,
  Processando,
  Processado = 9, // Only these two are needed since enums auto-increment and this one was out of order
  Agendado = 3,
  Protocolizado,
  Suspenso,
  "Erro protocolo",
  "Erro processamento",
  "Erro leitura",
};

export function constantFormatter(params: {
  value: number
}): string {
  return STATUS[params.value];
}

Answered by Elias Schablowski on November 15, 2021

You are trying to access a property that doesn't exist ('params').

You have an array with one object only. And this object doesn't have a params property. params is the variable with the index value. In order to access an object with a variable value, you can't use object.whatever. You have to use object[whatever].

Here:

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 constantFormatter(params) {
    return STATUS[0][params];
}

console.log(constantFormatter(4));

Answered by Nelson Teixeira on November 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP