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

237
Vistas
Search For Exact Match In JSON With JavaScript

I have been trying to figure out how have a form submit which then checks all the data in the form against JSON array data to determine if an object which matches all the input is already present. To start, here is my sample JSON data:

[
{
    "ASIN":"B0971Y6PQ3",
    "price":"13.99",
    "email": "test@gmail.com"
},
{
    "ASIN":"B077TLGP58",
    "price":"13.99",
    "email":"test@gmail.com"
}
]

So I am trying to run a for loop which will test whether all the form data already exists as a JSON object. Here's what I currently have:

// Check to see if it's already in asinJSON.json
for(i=0; i<asinJSON.length;i++){
    if(asinJSON[i].email == email){
        // Email is already in json
        if(asinJSON[i].ASIN == inputVal){
            // Email && ASIN are already in json
            if(asinJSON[i].price == desiredPrice){
                // Email, ASIN, Price all match. Duplicate.
                console.log('same price found. product already exists.');
                break;
            }
            // If price doesn't match, user wants to update price
            console.log('updating price');
            // Update price here
            // updateJSON();
            break;
        }
        // Existing user wants to add new product.
        console.log('product not found');
        // Insert product for existing user
        // createAndAdd();
        break;
    }
    // New user wants to add a product.
    console.log('email not found.');
    // insert product for new user
    // createAndAdd();
    break;
}

How it is now, when trying to test whether it can find the second object, it console.logs "product not found," which I understand is because it passes the first if statement but fails the second with the 1st object in the JSON array.

I'm also assuming it has to do with my break statements, and that something is wrong there. I've also tried return statents and haven't been able to figure it out. I've been self-taught so there are, unfortunately, some things that I have definitely missed along the way. But, I have looked aroung Google and StackOverflow and haven't really been able to find an answer, so here I am.

I'm ready to be schooled in how this logic should be set up to get it to work properly. I appreciate all the feedback in advance!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use the find() method to search for a matching element.

if (asinJSON.find(({ASIN, price, email: json_email}) => 
    ASIN == inputVal && price == desiredPrice && json_email == email)) {
    console.log("product already exists");
} else {
    console.log("product not found");
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

There are many options, one easy one is to use lodash

const email = <email to find>
const price = <price to find> (however, keep your mind around floating points comparison here...)
const ASIN = < ASIN to find >
if (findIndex(asinJSON, { ASIN, price, email }) > -1) {
   // found
}
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