Filter the month inside an object extracted with Object.keys

0

I am studying react and made a cash flow application using firebase as database. I would like to make a query using "map ()" or some other way I would run the object showing only the entries of the month of sea , but I'm finding it difficult to do this task, in view of the fact that the query using Object.keys . Below is the example of how the object gets after it has been extracted.

const keysIn = Object.keys(this.props.inflow);

let entrada =  JSON.stringify(keysIn);


console.log(entrada)

// O retorno do console.log fica assim
 {
    '1': {
      month: 'fev',
      name: 'xxxxxxx',
      value: 300,
    },
    '2': {
      month: 'mar',
      name: 'yyyyyyyy',
      value: 500,
    },
    '3': {
      month: 'mar',
      name: 'zzzz',
      value: 400,
    },
  }
    
asked by anonymous 28.09.2018 / 15:38

1 answer

2

In your case you can use map to form a array and after that use filter to keep only those that are with mar in attribute month :

const original = {
  '1': {
    month: 'fev',
    name: 'xxxxxxx',
    value: 300,
  },
  '2': {
    month: 'mar',
    name: 'yyyyyyyy',
    value: 500,
  },
  '3': {
    month: 'mar',
    name: 'zzzz',
    value: 400,
  },
};

// Pega apenas as chaves dos atributos
const chaves = Object.keys(original);
// Transforma o objeto em array baseado nas chaves
const todos = chaves.map((chave) => original[chave]);
// Filtra aqueles com a propriedade month igual a "mar"
const filtrados = todos.filter((item) => item.month === 'mar');

console.log(filtrados);
    
28.09.2018 / 16:14