javascript - How can I delete a target json by key value? -
[object, object, object, object]
object{ id:33, val:'a' }, object{ id:56, val:'b' }, object{ id:77, val:'a' }, object{ id:89, val:'d' }
now want delete object id = 56
how can using js?
there result want.
[object, object, object]
object{ id:33, val:'a' }, object{ id:77, val:'a' }, object{ id:89, val:'d' }
thanks.
how using delete? please note index order won't preserved this.
var text = '{"objects" : [{ "id":33, "val":"a"}, { "id":56, "val":"b"}, { "id":77, "val":"c"}, { "id":89, "val":"d"}]}'; var jsondata = json.parse(text); (var = 0; < jsondata.objects.length; i++) { var object = jsondata.objects[i]; if(object.id == 56) { delete jsondata.objects[i]; } }
if want preseve index order utilize below code.
var text = '{"objects" : [{ "id":33, "val":"a"}, { "id":56, "val":"b"}, { "id":77, "val":"c"}, { "id":89, "val":"d"}]}'; var jsondata = json.parse(text); (var = 0; < jsondata.objects.length; i++) { var object = jsondata.objects[i]; if(object.id == 56) { jsondata.objects.splice(i, 1); } }
javascript json object
No comments:
Post a Comment