Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

290
Vistas
How to filter JSON objects?

I have to filter JSON by parameters.

Get method : http://localhost:5000/api/car?bodyTypeId=2 (I need to get JSON objects only with bodyTypeId = 2. But unfortunately i getting them all):

[
    {
         "id": 1,
         "bodyTypeId": 1,  //bodyTypeId not equal to 2
         "carManufacturerId": 1
    },
    {
        "id": 2,
        "bodyTypeId": 2,
        "carManufacturerId": 1
    },
    {
        "id": 3,
        "bodyTypeId": 2,
        "carManufacturerId": 1
    }
]

Controller:

async getAll(req, res){  //filter
        let {carManufacturerId, bodyTypeId} = req.body
        let cars;
        if (!carManufacturerId && !bodyTypeId){
            cars = await Car.findAll()
        }
        if (carManufacturerId && !bodyTypeId){
            cars = await Car.findAll({where: {carManufacturerId}})
        }
        if (!carManufacturerId && bodyTypeId){
            cars = await Car.findAll({where: {bodyTypeId}})
        }
        if (carManufacturerId && bodyTypeId){
            cars = await Car.findAll({where: {bodyTypeId,carManufacturerId}})
        }
        return res.json(cars)
    }

Router:

router.get('/', CarController.getAll)
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your data is an array so you can use filter method. check here https://www.w3schools.com/jsref/jsref_filter.asp

const data = [
    {
         "id": 1,
         "bodyTypeId": 1,  
         "carManufacturerId": 1
    },
    {
        "id": 2,
        "bodyTypeId": 2,
        "carManufacturerId": 1
    },
    {
        "id": 3,
        "bodyTypeId": 2,
        "carManufacturerId": 1
    }
]

let result = null
data.filter(json => {
    if(json.bodyTypeId = 2) {
      result = json
    }
})

return result
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you just want to filter from an array of objects you can use the Array.filter method.

const allCars = [
    {
         "id": 1,
         "bodyTypeId": 1,  
         "carManufacturerId": 1
    },
    {
        "id": 2,
        "bodyTypeId": 2,
        "carManufacturerId": 1
    },
    {
        "id": 3,
        "bodyTypeId": 2,
        "carManufacturerId": 1
    }
]

const result = allCars.filter(car => car.bodyTypeId == bodyTypeId);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda