InsideDarkWeb.com

filter method returns empty array

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?

Stack Overflow Asked on November 15, 2021

3 Answers

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!

Related Questions

Find match pattern and delete the first occurrence

4  Asked on December 8, 2020 by neha-choudhary

   

Weird scoping behavior in python

1  Asked on December 8, 2020 by snickerdoodles777

     

Sqlite foreign key mismatch?

1  Asked on December 8, 2020 by daisy

     

pandas remove for loop

1  Asked on December 8, 2020 by arne

     

Change File with extension txt to json in C#

0  Asked on December 8, 2020 by azeter09

         

Appending an item from one list to another

3  Asked on December 8, 2020 by retroactive

       

Combine two Xcode targets into one distributable app

1  Asked on December 8, 2020 by thecoolwinter

         

Sorting multi-index pd.Series using pd.Categorical?

2  Asked on December 8, 2020 by np8

   

Trying to map an Array to an Object – Javascript

3  Asked on December 7, 2020 by mike-varela

   

Unable to ATTACH a second DB with SQLite Database and JDBC

1  Asked on December 7, 2020 by tacitus86

     

Create 3×3 responsive layout with CSS grid

1  Asked on December 7, 2020 by smithy

   

SQL query based on three tables with restriction

2  Asked on December 7, 2020 by derrick

 

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.