Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda