Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!