jquery - What is the best way to delete object from complex JavaScript object -


i have javascript object looks this:

{     "filters": [         {             "name": "test",             "items": [                 {                     "id": 1207700620,                     "checked": false                 },                 {                     "id": 1207825584,                     "checked": false                 },                 {                     "id": 1207969166,                     "checked": true                 }             ]         },         {             "name": "empty",             "items": []         },         {             "name": "thirdlist",             "items": [                 {                     "id": "1207828314",                     "checked": true                 },                 {                     "id": "1236723086",                     "checked": false                 },                 {                     "id": "1208005603",                     "checked": true                 }             ]         }     ] } 

my object have array name filters. array contains multiple objects. within each object there array name items. each element in items array, need check checked property & if false, need delete record (that item in items array).

i need ensure ie 8 compatability.

i can use loop on filters array , within loop can use loop go on items array. dont want apporach.

is there smarter way achieve lodash or jquery or other javascript library??

you can try combination of foreach , filter methods:

obj.filters.foreach(function(filter) {     filter.items = filter.items.filter(function(el) {         return el.checked;     }); }); 

check example below.

var obj = {      "filters": [          {              "name": "test",              "items": [                  {                      "id": 1207700620,                      "checked": false                  },                  {                      "id": 1207825584,                      "checked": false                  },                  {                      "id": 1207969166,                      "checked": true                  }              ]          },          {              "name": "empty",              "items": []          },          {              "name": "thirdlist",              "items": [                  {                      "id": "1207828314",                      "checked": true                  },                  {                      "id": "1236723086",                      "checked": false                  },                  {                      "id": "1208005603",                      "checked": true                  }              ]          }      ]  };    obj.filters.foreach(function(filter) {      filter.items = filter.items.filter(function(el) {          return el.checked;      });  });    alert( json.stringify(obj, null, 4) );

for ie8 compatibility can use polyfills or es5 shims.

another option if can use underscore/lo-dash, can go analog methods: _.each , _.filter instead of array.prototype.foreach , array.prototype.filter:

_.each(obj.filters, function(filter) {     filter.items = _.filter(filter.items, function(el) {         return el.checked;     }); }); 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -