JavaScript Best way to splice 2 keys from a javascript array

I have the following Object:

var obj = { "2014": {}, "2013": {}, "2012": {}, "description": null, "image": null },
    objKeys = Object.keys(obj);
//objKeys contains [ "2014", "2013", "2012", "description", "image" ]

I would like to remove the "description" and "image" from the objKeys array in one go if they exists there.

How can I achieve that ?

Answer:1

You can filter them out like this:

objKeys = objKeys.filter(function(x){ return !/[a-z]/gi.test(x)});

The above uses the fact that other keys are numbers. If you want only description and image, then put them in an array and do this:

var removeKeys = ["description","image"];
objKeys = objKeys.filter(function(x){ return !new RegExp(removeKeys.join("|")).test(x)});
Answer:2

Are the keys you wish to keep always numeric? I'm assuming they're "years"...

objKeys.filter(Number); // ["2012", "2013", "2014"]
Answer:3

You can filter them directly using regex.

objKeys = objKeys.filter(function(k){ return !/(description|image)/gi.test(k)});
Answer:4

I had the same problem this person: ShareThis plugin not working in FancyBox title But now I have gotten it work, except the afterShow: function(){ stButtons.locateElements(); } Where in this code ...

I had the same problem this person: ShareThis plugin not working in FancyBox title But now I have gotten it work, except the afterShow: function(){ stButtons.locateElements(); } Where in this code ...

I'm trying to group with kendo grid, but I am facing a problem with the default functionality of kendo grouping. Kendo grid groups the columns but it doesn't differ records of "USA" with "usa", these ...

I'm trying to group with kendo grid, but I am facing a problem with the default functionality of kendo grouping. Kendo grid groups the columns but it doesn't differ records of "USA" with "usa", these ...

  1. kendo grid grouping angular
  2. kendo grid grouping mvc
  3. kendo grid grouping collapse all
  4. kendo grid grouping jquery
  5. kendo grid grouping demo
  6. kendo grid grouping example
  7. kendo grid grouping and paging
  8. kendo grid grouping header template
  9. kendo grid grouping server side
  10. kendo grid grouping sort order
  11. kendo grid grouping
  12. kendo grid grouping template
  13. kendo grid grouping not working
  14. kendo grid grouping aggregate
  15. kendo grid grouping row template
  16. kendo grid grouping event
  17. kendo grid grouping header
  18. kendo grid grouping order
  19. kendo grid grouping asp.net mvc
  20. kendo grid grouping sort

I have always seen code like this: (function(){ //code here })(); How does this code work? Which function receives which parameters? (function(factory){ //code here }(function($){ //other ...

I have always seen code like this: (function(){ //code here })(); How does this code work? Which function receives which parameters? (function(factory){ //code here }(function($){ //other ...

I guess the better question here is more general: how do I debug errors for deployed apps? Does Meteor have commands/logs I can look at? I'm really not sure where to begin because everything works ...

I guess the better question here is more general: how do I debug errors for deployed apps? Does Meteor have commands/logs I can look at? I'm really not sure where to begin because everything works ...

  1. exception in defer callback
  2. deferred exception callback