0

I have a Javascript object:

var errorMap = {
    100: 'You must enter a name.',
    200: 'You must enter an address.',
    300: 'You must enter a DOB.'
}

In another part of my code, I am receiving an array of error codes:

var errorCodes = [100, 200, 500, 600];

What I'd like to do is compare the errorCodes array to the errorMap object's keys and return all error codes that do not have a corresponding key in errorMap. In this case, I would like to get this back:

[500, 600]

How do I do this? I have access to jQuery.

Daniel T.
  • 37,212
  • 36
  • 139
  • 206

5 Answers5

7

Javascript for modern browsers (works in older browsers with a shim)

var missing = [100, 200, 500, 600].filter(function(v){
    return !errorMap.hasOwnProperty(v)
});

//missing = [500, 600]

jQuery in case legacy browser support is required and shim is not acceptable:

var missing = $.grep( [100, 200, 500, 600], function(v){
    return !errorMap.hasOwnProperty(v)
});

//missing = [500, 600]
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • [Link to MDN docs](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter), if anyone is wondering (includes polyfill, which is needed in IE8-). – Rob W Jul 11 '12 at 19:35
  • 1
    Unless someone does incredibly nasty stuff (extending `Object.prototype`) there's nothing wrong with using `in` - and it's much more readable! – ThiefMaster Jul 11 '12 at 19:36
  • 1
    @ThiefMaster agree. You can foolproof that with `for( var key in Object.prototype ) throw new Error("pls die");` – Esailija Jul 11 '12 at 19:36
  • 1
    Remember that `filter` is no supported in older browser, in particular in IE8 (assuming there are living beings who care about it :] ). – freakish Jul 11 '12 at 19:41
  • @freakish here's code for it https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter#Compatibility We need to make developers more aware of these already 3 year old functions which work fine in ie7-8 with user code – Esailija Jul 11 '12 at 19:42
  • @freakish I have to care about IE7 :9 –  Jul 11 '12 at 19:43
  • @Esailija ...and why are you showing me this?? :] This doesn't change the fact that it is not supported in older browsers. – freakish Jul 11 '12 at 19:43
  • @freakish ok I can use jQuery for it. But this just slows down adoption of perfectly fine and shimmable native methods. – Esailija Jul 11 '12 at 19:44
  • @Esailija No, no, no. This is *JavaScript* problem, not *jQuery* problem. And it should be solved as *JavaScript* problem. And what about this `filter` talk and making developers aware? Please, go back to the previous code or I'll be forced to give you -1 for this. :( I only noted that `filter` is not supported, not that it shouldn't be used!!! – freakish Jul 11 '12 at 19:50
  • @freakish sorry mate but I don't know how to make you happy. `.filter` with a shim link is not enough but `$.grep` is also not ok, even when the op is using jQuery? – Esailija Jul 11 '12 at 19:52
  • jQuery just translates to Javascript. I prefer to use whichever solution is cleaner, regardless of performance. The largest the error code array will ever get is 8 items. – Daniel T. Jul 11 '12 at 19:53
  • @Esailija `filter` does make me happy. :) You misunderstood me. The point of my comment was to make people aware of the support issue. I totally agree, that `filter` is the way to do that. – freakish Jul 11 '12 at 19:53
  • @DanielT. jQuery does translate to JavaScript but what will you do if you face similar problem and you won't be able to use jQuery? Stick with pure JavaScript whenever it is possible. – freakish Jul 11 '12 at 19:54
  • @freakish I'll cross that bridge when I get there. Sticking to pure JS in this day and age for the sake of 'backwards compatibility' is like writing code for Netscape Navigator. – Daniel T. Jul 11 '12 at 19:56
  • @DanielT. It is not for the sake of backwards compatibility. It is because JavaScript is a language and jQuery is not. For example JavaScript is forcing it's way to the server side nowadays. And I wonder: how many (so called) JavaScript developers (read: jQuery developers) will be able to work in that environment? Where jQuery is not available (not exactly true, but close enough)? – freakish Jul 11 '12 at 20:00
  • Guys we do have a [chatroom](http://chat.stackoverflow.com/rooms/17/javascript) as well, you know? :P – Esailija Jul 11 '12 at 20:00
  • @freakish It's the same as before, I'll cross that bridge when I get there. If I have access to jQuery, I will use it. Take it away, and I'll use something else. You're acting like developers are incapable of adapting, and that if you take a tool away from them, they won't be able to work anymore. You mentioned to 'Stick with pure JavaScript whenever it is possible', but that's **always** possible. With your two statements, it really does sound like you're advocating **not** using any frameworks whatsoever. – Daniel T. Jul 11 '12 at 20:17
  • @DanielT. Believe me: most of developers **are** incapable of adapting (or at least it is so difficult for them). I am not against frameworks. When I say `use JavaScript wheneve it is possible` I have something else in mind. What are frameworks for? Generally to shorten your code. But how does jQuery shorten your code here?? And why would you use something like `grep` (I don't even know what that word means, although english is not my native language) instead of something plain and simple like `filter`? Finally this is a matter of opinion, so you are free to code however you like. Cheers! – freakish Jul 12 '12 at 06:39
1

jQuery is not really necessary, a simple for loop does the job, too:

var missing = [];
for(var i = 0; i < errorCodes.length; i++) {
    var code = errorCodes[i];
    if(errorMap[code] === undefined) {
        missing.push(code)
    }
});

With jQuery:

var missing = [];
$.each(errorCodes, function(i, code) {
    if(errorMap[code] === undefined) {
        missing.push(code)
    }
});
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0
var notRealError = [];
for(var i = 0; i < errorCodes.length; ++i){
    if(!errorMap[errorCodes[i]])  notRealError.push(errorCodes[i]);
}

notRealError is an array that contains the error codes that are not in your map.

Naftali
  • 144,921
  • 39
  • 244
  • 303
0

No need for jQuery with this one. Try this:

var result = [];
var l = errorCodes.length;
for (var i = 0; i < l; i++)
    if (!errorMap.hasOwnProperty(errorCode[i]))
        result.push(errorCode[i]);
freakish
  • 54,167
  • 9
  • 132
  • 169
0
var returnCodes = [];

for (c in errorCodes) {
    if (errorMap[errorCodes[c]] == undefined) {
        returnCodes.push(errorCodes[c]);
    }
}

return returnCodes;
Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106