Questions tagged [hasownproperty]

hasOwnProperty is a method of the object object in JavaScript. It is used to check whether the calling object has the specified key as a direct member. It does not traverse the prototype chain.

hasOwnProperty is a method of most objects. A notable exception is the window object in IE.

116 questions
2
votes
1 answer

JavaScript: hasOwnProperty vs dot syntax

Imagine there is an object: foo = {"bar": 1} Is there any benefit to use hasOwnProperty over dot-syntax to check for bar property inside foo object: if (foo.hasOwnProperty('bar') { // do something } vs if (foo.bar) { // do something } Also:…
0leg
  • 13,464
  • 16
  • 70
  • 94
2
votes
1 answer

what's the difference with hasOwnProperty and in?

When I have an array like var user = { name : 'Bob'}; what is the difference when using the following instruction? localuser = user.name; localuser = 'name' in user ? user.name : 'bob'; localuser = user.hasOwnProperty('name') ? user.name : 'bob'
Haven
  • 7,808
  • 5
  • 25
  • 37
2
votes
1 answer

Object.getOwnPropertyNames() vs Object.prototype.hasOwnProperty()

Both of these obviously do similar things but my question is why is one on the prototype and one on the Object? For example, both of these called differently. Is there a logical reason why this is the case? var o = {name:…
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
1
vote
2 answers

"Object doesn't support this property or method IE" error in Javascript possibly from using hasOwnProperty?

I'm getting this error in IE8 and IE7 for some reason. I'm looping through all keys within my object and it keeps telling me Object doesn't support this property or method on this on: var inVal = $(inType + "#" + inName).val().trim(); The entire…
bob_cobb
  • 2,229
  • 11
  • 49
  • 109
1
vote
2 answers

Removing duplicates in array in javaScript using hasOwnProperty

I have an array with duplicate elements. While trying to remove duplicate elements using hasOwnProperty getting one duplicate element in array rest of duplicate element removed successfully. expexted output = [1, 3, 2, 4, 5, 6, 7] but getting…
NewUser
  • 65
  • 1
  • 5
1
vote
1 answer

JSON - return number of items within a list that have a specific key value

Having a bit of a nightmare trying to solve this JSON problem. I need to store the number of offers as a tally, but it needs to only be a number whereby the offers have an activated status. In the example below (simplified for illustration) the…
Chuwa Feet
  • 49
  • 5
1
vote
2 answers

TypeError: Cannot read property 'hasOwnProperty' of undefined - Twitter API V2 vs. GAS

Twitter API: https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/introduction In theory, when there is no data, it should leave the value as empty. As seen in this part of the script: if ( …
1
vote
0 answers

React hasOwnProperty does not find property

I have a problem finding a property that I know is inside the JSON response. I have this: { "global": { "loading": false, "error": false, "currentUser": false, "userData": { "repositories": false } }, "language": { …
1
vote
1 answer

Check if a json key is a complex object javascript

USING IBP BPM 8.6: I have a Json object as follows: tw.local.stringifiedJSON = "{"name":"ahmed","age":"20","job":{"salary":"1000","position":"developer"}}"; I parsed into a javascript object: var parsedJSONTW=…
1
vote
5 answers

Which object does not have `hasOwnProperty` in JavaScript?

With some values, calling hasOwnProperty throws an error. Let's check the following code: null.hasOwnProperty('bar') // Error undefined.hasOwnProperty('bar') // Error (0).hasOwnProperty('bar') // Returns false Are there any other variables rather…
Sang
  • 4,049
  • 3
  • 37
  • 47
1
vote
6 answers

How to check chain of hasOwnProperty in objects

I have json like this var a = { "name": "test1", "redisData": { "redisIp": "127.0.0.1", "dbSetting": { "dbIp": "127.0.0.1", "dbUserName": "root", "dbUserPassword": "root", }, …
Dexter
  • 1,804
  • 4
  • 24
  • 53
1
vote
1 answer

Filer Array of Objects based on key/value pair

I'm creating a function that takes the first argument, an array of objects, and compares it to the second argument (an object) to see if the key/value pair match. The function would return the object that contains the matching key/value pair. I'm…
Pbculley
  • 79
  • 8
1
vote
0 answers

How come properties of a SpeechRecognitionAlternative object are not its own?

Below is a simple demo of how I am using the Web Speech API for speech recognition. If you run the demo yourself, just say something after you give mic permissions and watch the console: var recognition = new…
1
vote
1 answer

How can I access columns.roles in Power BI development?

Could not find this answer online, so decided to post the question then the answer. I created a table in the capabilities.json file: "dataRoles": [ { "displayName": "Stakeholders", "name": "roleIwant", "kind":…
workaholic
  • 73
  • 10
1
vote
3 answers

How to bind functions to the JSON object?

function Person(_name, _id, _salary){ this.Name = _name; this.Id = _id; this.Salary = _salary; } Person.prototype.f_IncreaseSalary = function( _percentage ){ this.Salary *= _percentage; } var per = new Person("cem",10,15000); 1) I…
uzay95
  • 16,052
  • 31
  • 116
  • 182