i lost lot of time trying delete item json file
this json file
{ "count": 3, "data": [ { "id": "1", "raison": "abs", "email": "abs@abs.com", "tel": "021269999999" }, { "id": "3", "raison": "podl", "email": "abs@abs.com", "tel": "021269999999" }, { "id": "5", "raison": "ddms", "email": "abs@abs.com", "tel": "021269999999" } ] }
in controller have
$scope.deleterow = function() { var _id = 3; $scope.datas = json.stringify($scope.clients["data"]); $scope.datas.splice(_id,1); };
so gave _id want remove error $scope.datas.splice splice not function, tried simple file worked fine ["aaa","bob,"ccc"]
using indexof()
, splice in file json non :(
how in javascript can know/compare right id ?
so me , , lot .
you're getting error because splice() array method, while datas
object string. might confusing slice() method, indeed defined strings.
so, cale_b pointed out, there no need stringify json object. leaving array give more functionality. example, if want remove second element in clients.data
, can write:
$scope.clients["data"].splice(1, 1);
and if wanted remove value based on id
property, , not position of object in data
array, can search using indexof() , find() methods, so:
$scope.clients["data"].splice( $scope.clients["data"].indexof( $scope.clients["data"].find(function(e) { return e.id == 2; })), 1);
Comments
Post a Comment