javascript - An object of array must contain specific members/names -


how can validate object of arrays that's passed parameter jquery plugin make sure contains specific members/names?

for example, want validate if object below has 'name', 'id' & 'location', 3 members, in case should true.

var arr = [{     name: 'johny',     id: 1,     location: 'usa' }, {     name: 'mike',     id: 4,     location: 'can' }]; 

thanks in advance.

johny

you can use array.prototype.every method test every object in array conforms necessary keys rules:

var valid = arr.every(function(obj) {     var keys = object.keys(obj);     return keys.length === 3 && 'name' in obj && 'id' in obj && 'location' in obj;  }); 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

Simple Angular 2 project fails 'Unexpected reserved word' -

ios - Possible to get UIButton sizeThatFits to work? -