compare and delete json data using javascript -
below json code:
{ "uid": "4564646", "favorites": [ { "name" : "my store", "id" : "87654321", "items": [ { "productid": "46565", "title": "project", "type": "weekend project" }, { "productid": "112", "title": "bathroom", "type": "weekend project" }, { "productid": "785", "title": "link", "type": "main project" } ] } ] }
now want check productid each , delete entry items matches product id 112 . want utilize javascript only, array items not fixed , have parse json before applying deletion method.
var obj = json.parse(json_string); var fav_items = obj.favorites[0].items; (var = fav_items.length - 1; >= 0; i--) { if (fav_items[i].productid == 112) { fav_items.splice(i, 1); } }
note need loop backwards, because when splice out element array, elements after there indexes shifted down. if normal upward loop, you'll skip on elements after ones delete.
javascript json
No comments:
Post a Comment