Add another object from a file to a Javascript Object Array -
im trying build cart functionality user add single product array of products or objects.
var product = { "name": "bike", "price": "900", "idproduct": "1" }; now store product in plain json text.stringify function that.
var jsonstring = json.stringify(product); now take jsonstring , convert object or object array so.
var jsonobjectarray = json.parse(jsonstring); now im going create new same json object type diff values.
var product2 = { "name": "car", "price": "12000", "idproduct": "2" }; it seems jsonobjectarray doesnt have push method because got deserialized single object namely product1, should can add product2 jsonobjectarray member of aray , repeat process multiple times.
jsonobjectarray.push(product2);
starting product have defined it, want 1 of following:
use
json.stringify([product])jsonstringjson array:var jsonstring = json.stringify([product]); var jsonobjectarray = json.parse(jsonstring);leave
jsonstringsingle object, create 1 element array result ofjson.parse:var jsonstring = json.stringify(product); var jsonobjectarray = [json.parse(jsonstring)];
Comments
Post a Comment