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.

6
  • possible duplicate of Javascript array difference Commented Jul 11, 2012 at 19:32
  • Check out underscorejs.org Commented Jul 11, 2012 at 19:33
  • @Jack what is the need for another library? Commented Jul 11, 2012 at 19:37
  • I am sure this is a duplicate, but I don't think of that question .. Commented Jul 11, 2012 at 19:42
  • 1
    @Engineer I ask -a lot- of questions :). Commented Jul 11, 2012 at 19:49

5 Answers 5

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]
Sign up to request clarification or add additional context in comments.

18 Comments

Link to MDN docs, if anyone is wondering (includes polyfill, which is needed in IE8-).
Unless someone does incredibly nasty stuff (extending Object.prototype) there's nothing wrong with using in - and it's much more readable!
@ThiefMaster agree. You can foolproof that with for( var key in Object.prototype ) throw new Error("pls die");
Remember that filter is no supported in older browser, in particular in IE8 (assuming there are living beings who care about it :] ).
@freakish here's code for it developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… We need to make developers more aware of these already 3 year old functions which work fine in ie7-8 with user code
|
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)
    }
});

4 Comments

@Neal the question is tagged jQuery!
Exactly. Besides that, I just added a non-jQuery version.
@ThiefMaster: following your own comment: what if errorMap maps an error to undefined? The only way to do this is with hasOwnProperty.
that is really unlikely (well, ok, the empty string is also rather unlikely)
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.

1 Comment

@ThiefMaster hmm that is true. but I am using the OP's definition of just using numbers.
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]);

Comments

0
var returnCodes = [];

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

return returnCodes;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.