Get value from object in javascript -


i have several objects structure: enter image description here

then create function update quantity of item matched search id :

function setquantityincartbyid(json, itemid, val){     for(var in json){         console.log(json[i].id);         if(json[i].id == itemid){            json[i].quantityincart = val;            break;         }      }      return json; } 

this json ;

{"departmentid":12,"categoryid":117,"brandid":19,"brandimage":"         ","brandname":"general","id":708,"name":"grand 6port power center","picturename":"http://111.92.241.110/wwwproducts/unknown.png","notes":"","priceshow":"$10.00","price":0,"pricea":0,"priceb":0,"pricec":0,"websiteprice":0,"quantity":2,"quantityincart":2,"lastupdated":"/date(1378108144050)/","active":1,"pricelevel":0,"newproductimage":"http://111.92.241.110/wwwproducts/newproduct.png","isnewproduct":false,"isinstock":"10 available in stock(s)!","newarrival":0,"expirenewarrival":"/date(-62135596800000)/","newpromotion":0,"expirenewpromotion":"/date(-62135596800000)/"}  {"departmentid":12,"categoryid":117,"brandid":19,"brandimage":"         ","brandname":"general","id":709,"name":"grand 6port power center","picturename":"http://111.92.241.110/wwwproducts/unknown.png","notes":"","priceshow":"$10.00","price":0,"pricea":0,"priceb":0,"pricec":0,"websiteprice":0,"quantity":2,"quantityincart":2,"lastupdated":"/date(1378108144050)/","active":1,"pricelevel":0,"newproductimage":"http://111.92.241.110/wwwproducts/newproduct.png","isnewproduct":false,"isinstock":"10 available in stock(s)!","newarrival":0,"expirenewarrival":"/date(-62135596800000)/","newpromotion":0,"expirenewpromotion":"/date(-62135596800000)/"}         {"departmentid":12,"categoryid":117,"brandid":19,"brandimage":"         ","brandname":"general","id":710,"name":"grand 6port power center","picturename":"http://111.92.241.110/wwwproducts/unknown.png","notes":"","priceshow":"$10.00","price":0,"pricea":0,"priceb":0,"pricec":0,"websiteprice":0,"quantity":2,"quantityincart":2,"lastupdated":"/date(1378108144050)/","active":1,"pricelevel":0,"newproductimage":"http://111.92.241.110/wwwproducts/newproduct.png","isnewproduct":false,"isinstock":"10 available in stock(s)!","newarrival":0,"expirenewarrival":"/date(-62135596800000)/","newpromotion":0,"expirenewpromotion":"/date(-62135596800000)/"} 

problem : console.log(json[i].id);result undefined.

the previous link did not seem apply after made fiddle test out. updated first example.

http://jsfiddle.net/afzht/

this assumes you've done var json = json.parse(data)

if returning array of objects, data should similar below. passed setquantityincartbyid function json parameter:

var json = [{"departmentid":12,"categoryid":117,"brandid":19,"brandimage":"         ","brandname":"general","id":708,"name":"grand 6port power center","picturename":"http://111.92.241.110/wwwproducts/unknown.png","notes":"","priceshow":"$10.00","price":0,"pricea":0,"priceb":0,"pricec":0,"websiteprice":0,"quantity":2,"quantityincart":2,"lastupdated":"/date(1378108144050)/","active":1,"pricelevel":0,"newproductimage":"http://111.92.241.110/wwwproducts/newproduct.png","isnewproduct":false,"isinstock":"10 available in stock(s)!","newarrival":0,"expirenewarrival":"/date(-62135596800000)/","newpromotion":0,"expirenewpromotion":"/date(-62135596800000)/"},      {"departmentid":12,"categoryid":117,"brandid":19,"brandimage":"         ","brandname":"general","id":709,"name":"grand 6port power center","picturename":"http://111.92.241.110/wwwproducts/unknown.png","notes":"","priceshow":"$10.00","price":0,"pricea":0,"priceb":0,"pricec":0,"websiteprice":0,"quantity":2,"quantityincart":2,"lastupdated":"/date(1378108144050)/","active":1,"pricelevel":0,"newproductimage":"http://111.92.241.110/wwwproducts/newproduct.png","isnewproduct":false,"isinstock":"10 available in stock(s)!","newarrival":0,"expirenewarrival":"/date(-62135596800000)/","newpromotion":0,"expirenewpromotion":"/date(-62135596800000)/"} ,            {"departmentid":12,"categoryid":117,"brandid":19,"brandimage":"         ","brandname":"general","id":710,"name":"grand 6port power center","picturename":"http://111.92.241.110/wwwproducts/unknown.png","notes":"","priceshow":"$10.00","price":0,"pricea":0,"priceb":0,"pricec":0,"websiteprice":0,"quantity":2,"quantityincart":2,"lastupdated":"/date(1378108144050)/","active":1,"pricelevel":0,"newproductimage":"http://111.92.241.110/wwwproducts/newproduct.png","isnewproduct":false,"isinstock":"10 available in stock(s)!","newarrival":0,"expirenewarrival":"/date(-62135596800000)/","newpromotion":0,"expirenewpromotion":"/date(-62135596800000)/"}]; 

in page, following receive json object above , should have length on it.

function setquantityincartbyid(json, itemid, val){  for(var in json){         console.log(json[i].id);         if(json[i].id == itemid){            json[i].quantityincart = val;            break;         }      }      return json; } 

here's old way seem have been thinking of:

function setquantityincartbyid(json, itemid, val){     for(var = 0; <json.length;i++)     {         console.log(json[i].id);         if(json[i].id === itemid){            json[i].quantityincart = val;            break;         }      }      return json; } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -