0
var _ = require('lodash');

var users = [
  { 'id': '1', 'coins': false },
  { 'id': '2', 'coins': false }
];

var a = _.every(users, function(p){
  if ('id' in p && 'coins' in p)
    return true;
  else
    return false;
});
console.log(a);

The function works to check in keys exists in an array of objects. If one of the object doesn't exists "id" or "coins" , it return false.

Is there a better way to write thie snippet of code? I felt quite clumsy.

4 Answers 4

3

Since you're in node.js, you know you already have array.every() so I don't see any reason for lodash here or for the if/else. Why not this:

var users = [
  { 'id': '1', 'coins': false },
  { 'id': '2', 'coins': false }
];

var allValid = users.every(function(item) {
    return 'id' in item && 'coins' in item;
});

FYI, this code is assuming nobody has mysteriously added properties named id or coins to the Object.prototype (which seems like a safe assumption here). If you wanted to protect against that, you could use item.hasOwnProperty('id') instead of in.

Sign up to request clarification or add additional context in comments.

2 Comments

@RobG - I will add that caveat, but these are literals so .hasOwnProperty() does not seem necessary here.
Thanks a lot for this explanation!
2

At very least, replace:

if ('id' in p && 'coins' in p)
    return true;
else
    return false;

With:

return 'id' in p && 'coins' in p;

Basically, never use a construct like:

if (x)
    return true;
else
    return false;

x is already a boolean, or at least a truthy / falsy value.

In case you need to be sure the returned value is a boolean, just force it to one:

return !!('id' in p && 'coins' in p);

Also, as mentioned in the other answer, lodash is redundant, here. You canuse JS's native [every][3].
Replace:

_.every(users, function(p){

With:

users.every(function(p){

4 Comments

Well, x is not necessarily a boolean... It is in this particular case, but not always.
You don't need the !! here. The in operator already returns a boolean.
@jfriend00: I just added that as an explanation that always replacing an if statement like that can sometimes result in unexpected responses.
The in relational operator returns true/false, it's the comment in regard to the if (x) test that wasn't correct.
0

You can use _.has() to check if an object property exists:

function checkValidity(array, listOfKeys) {
    return _.every(array, function (item) {
        return _.every(listOfKeys, function (key) {
            return _.has(item, key);
        });
    });
}

Usage:

checkValidity(users, ['id', 'coins']);

Comments

0

I'd use the [Array.prototype.some()][1] function:

 var users = [
  { 'id': '1', 'coins': false },
  { 'id': '2', 'coins': false }
];


    var result = users.some(e => e.hasOwnProperty('id') && e.hasOwnProperty('coins'));

    console.log("The array contains an object with a 'name' and 'quantity' property: " + result);

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.