2

I am creating a store. The way it works is that when I click 'add to cart', I run a function which gets the id of the button. From that id, you can work out the name of the product, get the cost, quantity etc. For simplicity, this is the funciton code:

function getID(a){

    var id = a.substring(0, a.length - 4);
    var id_quant = document.getElementById(id + '_val').value;
    var id_name = document.getElementById(id + '_nme').innerHTML;
    console.log(id + " " + id_quant + " " + id_name);   

}    

You may notice that I remove the last 4 digits from this.id, this is because when I run the PHP to echo all the results, i make the button_id equal to the uid from the database, concatenated with _btn.

What I would then like to do is to push this into an array, or use JSON.

What I am imagining is a nest array like : array[uid][cost], array[uid][name]. I'm just not sure how to do this.

It should be noted that in the function, a is actually this.id:

<button id='1_btn' onclick='getID(this.id)'></button>

I have declared an array above the function, var array = [].

Any help on how to push a nest would be great.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
N. Sainsbury
  • 137
  • 2
  • 15
  • Don't put tags in titles. Also, there's no JSON in your question, so the [tag:json] is irrelevant. – T.J. Crowder Feb 19 '18 at 10:07
  • @T.J.Crowder Rather than assist with the question, you nitpick. I asked int he question, about using JSON. I believed it may have been relevant, 'JavaScript Object Notation'. Considering the responses suggest using an object formatted with JSON, I would think it was relevant. – N. Sainsbury Feb 19 '18 at 10:20
  • Constructive feedback != nitpicking. You're newish to the site, so I helped you understand how the site works. Also, again, there is no JSON in the question, nor is there any in the answers. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. (This is also constructive feedback, btw -- now you know that you're misusing the term JSON, which will help you when you search for solutions to problems in future.) – T.J. Crowder Feb 19 '18 at 10:23

2 Answers2

4

What I would then like to do is to push this into an array, or use JSON.

You need to use an object

var obj = {};

and set values in this obj from the function as

function getID(a){
    var id = a.substring(0, a.length - 4);
    var id_quant = document.getElementById(id + '_val').value;
    var id_name = document.getElementById(id + '_nme').innerHTML;
    obj[ id ] = { quantity : id_quant, name : id_name }; //observe this line
    console.log(id + " " + id_quant + " " + id_name);   
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
1

instead of array you should have an object.

var bucket = {};

bucket[someuid] = {
"cost": 500,
"name": "Some Item" 
}

Now you can access this simply by,

console.log(bucket[someuid]);

Iterating then is simple

for (item in bucket){ console.log(item['name'], item['cost']) }
NnN
  • 463
  • 2
  • 11