Walter Purvis
Walter Purvis

Reputation: 25

Return json array if matches input

Need help: I have my JSON formatted array. What I can't seem to wrap my head around is how to all the contents of said array when it matches user input. I can display the whole array and i can check if the input is in the array.

Code:

//user input 
var product  = document.getElementById('product').value;
var price    = document.getElementById('price').value;
var quantity = document.getElementById('quantity').value; 

//Products
var viewInventory = [{
    id : 'a',
    name  : 'iphone',
    model : 10,
    price : 900,
    quantity : 25
}, {    
    id: 'b',
    name  : 'pixel',
    model : 1,
    price : 800,
    quantity : 40
},{
    id: 'c',
    name  : 'pants',
    model : 8,
    price : 700,
    quantity : 80
},{
    id: 'd',
    name  : 'essential',
    model : 10,
    price : 900,
    quantity : 25
}];//end of viewInventory

console.log(viewInventory);//just testing to see it JSON obj works




function hello(){
var item;


for (var i = 0; item = viewInventory[i].name; i++){
    console.log(item);
    // console.log(viewInventory[i].name)


    if (product === item){
        console.log(viewInventory[i]);

        console.log(item + "This is input");
        // document.write(myTable);
    }

}

Problem:

Below is the problem from my book ( i am self learning). Pulling data from a file into a complex data structure makes parsing much simpler. Many programming languages support the JSON format, a popular way of representing data.

Create a program that takes a product name as input and retrieves the current price and quantity for that product.The product data is in a data file in the JSON format and looks like this:

{
_"products" : [
_{"name": "Widget", "price" : 25.00, "quantity": 5 },
__{"name": "Thing", "price": 15.00, "quantity": 5},
__{"name": "Doodad", "price": 5.00, "quantity": 10}
__]
}

Print out the product name, price, and quantity if the product is found. If no product matches the search, state, that no product was found and start over.

Example output What is the product name? iPad Sorry, that product was not found in our inventory What is the product name? Widget Name: Widget Price: $25.00 Quantity on hand: 5

Constraints The file is in the JSON format. Use a JSON parser to pull the values out of the file. If no record is found, prompt again. Challenges Ensure that the product search is case-insensitive. When a product is not found, ask if the product should be added. If yes, ask for the price and the quantity, and save it in the JSON file. Ensure the newly added product is immediately available for searching without restarting the program.

Upvotes: 0

Views: 514

Answers (1)

Joshua K
Joshua K

Reputation: 2537

you can use the Array.prototype.filter function:

var product  = document.getElementById('product').value;
var price    = document.getElementById('price').value;
var quantity = document.getElementById('quantity').value; 

var matchingProducts = viewInventory.filter((v)=>v.name.indexOf(product)>-1);
if(!matchingProducts.length) alert('Sorry, that product was not found in our inventory');
else {
  alert(
    matchingProducts.reduce(
      (c,i)=>c+i.name+' found with price='+i.price+' and the quantity='+i.quantity+'\n',
      ''
    )
  );
}

Upvotes: 1

Related Questions