0

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.

3
  • 2
    What exactly is your question? So much text and I'm not sure what you're asking... Commented Sep 26, 2017 at 0:55
  • The part at the top of the block of text that reads " need help: " what im looking to do is take user input and match to json object. Then return all the properties in the array pertaining to the input. Like if i type iphone it will return all properties of iphone. Commented Sep 26, 2017 at 1:02
  • you need to create a onClick handler, pass the input text from the user as argument to your function, once it goes thru the loop, if it returns true once it iterates through the loop it should display the data, if false, it should exit the loop with another message Commented Sep 26, 2017 at 1:09

1 Answer 1

1

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',
      ''
    )
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.