ReactFix
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • React
  • React Native
  • Programming
  • Object Oriented Programming

Saturday, November 19, 2022

[FIXED] How can i filter a attribute from object from array of values in javascript

 November 19, 2022     arrays, filter, javascript, reactjs   

Issue

I need help. i need filter a json object like this...

data = 
[
{
    "Empresa": "PLANTA",
    "Sucursal": "AEROPUERTO",
    "Producto": "PIZZA",
    "Categoria": "RAPIDA"
    "Total": 84,
    "FPago": "TERMINAL",
    "SegCliente": "SIN SEGMENTO",
    "Origen": "MOSTRADOR"
},
{
    "Empresa": "RIVERA",
    "Sucursal": "CIRCUITO",
    "Producto": "PASTEL",
    "Categoria": "BOCADILLOS",
    "Total": 152,
    "FPago": "INALAMBRICA",
    "SegCliente": "SIN SEGMENTO",
    "Origen": "MOSTRADOR"
}
 ,...
]

I get a array from filtres from Categoria like this ['BOCADILLOS','POSTRES'...] and another to empresa like ['PLANTA',...] I built these arrays dynamically

How can i filter data.Categoria with values in ['BOCADILLOS','POSTRES'...]

ty for help


Solution

You can use Array.filter

let categories = ['BOCADILLOS', 'POSTRES'];
let filtered = data.filter((item) => categories.includes(item.Categoria));


Answered By - Benjamin
Answer Checked By - - David Goodson (ReactFix Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post Home

Featured Post

Is Learning React Difficult?

React is difficult to learn, no ifs and buts. React isn't inherently difficult to learn. It's a framework that's different from ...

Total Pageviews

Copyright © ReactFix