Home > Blockchain >  Search For Exact Match In JSON With JavaScript
Search For Exact Match In JSON With JavaScript

Time:12-20

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": "[email protected]"
},
{
    "ASIN":"B077TLGP58",
    "price":"13.99",
    "email":"[email protected]"
}
]

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!

CodePudding user response:

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");
}

CodePudding user response:

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
}
  • Related