0

I have an array which is stored in a local storage.

How do I make it multi dimensional e.g this allows me to store only one set of values

//STEP 1 - create a JSON Object 
var Basket = { ProductID: product, Quantity: quantity , Price: price , Total: total}

and how do i loop through the values and get the results, I am not sure how to get this done in javascripts but I have a vague idea

e.g.

//STEP 2 - create an array 
var BasketContents = new BasketContents[]


//STEP 3 - create array of objects
    BasketContents[i].push ... values here// how I (1) check if empty (2) or else insert/increment values into here dynamically


//step 4 - reiterate through my array
var results = "";
for (i=0;i =< BasketContents.length ; i++)
{
results = BasketContents[i].Basket //display contents here
}
chicken
  • 13
  • 4
  • How does Basket relate to BasketContents? `i >= Basket.length` should be using `<=` for sure though. – Gavin Sep 22 '19 at 15:39
  • sorry I corrected that, – chicken Sep 22 '19 at 16:09
  • Possible duplicate of [How to convert an Object {} to an Array \[\] of key-value pairs in JavaScript](https://stackoverflow.com/questions/38824349/how-to-convert-an-object-to-an-array-of-key-value-pairs-in-javascript) – StudioTime Sep 22 '19 at 16:29

1 Answers1

0

Did you just want to output the JSON result? The below code will do that. Please clarify if this is not what you want.

//STEP 1 - create a JSON Object 
var Basket = {
  ProductID: '1',
  Quantity: '100',
  Price: '333',
  Total: 'total'
}

//STEP 2 - create an array 
var BasketContents = [];

//STEP 3 - create array of objects
BasketContents.push(Basket);


// Output

// [
//   {
//     "ProductID": "1",
//     "Quantity": "100",
//     "Price": "333",
//     "Total": "total"
//   }
// ]
Tiberiuscan
  • 413
  • 4
  • 8