32

I wanted to check if the an object has a property of something and its value is equal to a certain value.

var test = [{name : "joey", age: 15}, {name: "hell", age: 12}]

There you go, an array of objects, now I wanted to search inside the object and return true if the object contains what I wanted.

I tried to do it like this:

Object.prototype.inObject = function(key, value) {
if (this.hasOwnProperty(key) && this[key] === value) {
  return true
};
return false;
};

This works, but not in an array. How do I do that?

Joey Hipolito
  • 3,108
  • 11
  • 44
  • 83

5 Answers5

47

Use the some Array method to test your function for each value of the array:

function hasValue(obj, key, value) {
    return obj.hasOwnProperty(key) && obj[key] === value;
}
var test = [{name : "joey", age: 15}, {name: "hell", age: 12}]
console.log(test.some(function(boy) { return hasValue(boy, "age", 12); }));
// => true - there is a twelve-year-old boy in the array

Btw, don't extend Object.prototype.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 3
    @Ecropolis: but trivially shimmable, see the "polyfill" section of the linked docs – Bergi Apr 01 '15 at 00:17
  • @poepje Oh right. Never certain in written communication, especially with so many newbies around who might pose this as a serious question. Now pointed out, the humour is obvious... – Bergi Sep 06 '17 at 21:40
6

-- for the property --

if(prop in Obj)  
//or
Obj.hasOwnProperty(prop)

-- for the value ---

Using "Object.prototype.hasValue = ..." will be FATAL for js but Object.defineProperty let you define properties with enumerable:false (default)

Object.defineProperty(Object.prototype,"hasValue",{
   value : function (obj){
              var $=this;
              for( prop in $ ){
                  if( $[prop] === obj ) return prop;
              }
              return false;
           }
});

just for experiment test if a NodeList has an Element

var NL=document.QuerySelectorAll("[atr_name]"),
    EL= document.getElementById("an_id");
console.log( NL.hasValue(EL) )  

// if false then #an_id has not atr_name
bortunac
  • 4,642
  • 1
  • 32
  • 21
  • This is the most useful answer I've found for checking an object for a value, and the only one that explicitly mentions the fatal javascript error that many people will encounter. – andy.holmes Jul 12 '17 at 17:36
4

For array, of course you have to browse that array with for

for(var i = 0 ; i < yourArray.length; i++){
    if(yourArray[i].hasOwnProperty("name") && yourArray[i].name === "yourValue") {
     //process if true
    }
} 
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
0

Typically you'll use something like Object.first:

// search for key "foo" with value "bar"
var found = !!Object.first(test, function (obj) {
    return obj.hasOwnProperty("foo") && obj.foo === "bar";
});

Assuming that Object.first will return some falsy value when it doesn't find a match.

Object.first is not a native function but check on of the popular frameworks, they're bound to have one.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

Here is another solution for checking if the object has the property but the value of property is not set. Maybe the property value has 0, null or an empty string.

array.forEach(function(e){
 if(e.hasOwnProperty(property) && Boolean(e[property])){
  //do something
 }
 else{
  //do something else
 }
});

Boolean() is the trick here.

Ali Hayder
  • 361
  • 4
  • 16