2

I am trying to find whether the value roomTypeFilter exists within an Object inside an array. I then want to perform conditional statements depending whether the value roomTypeFilter exists or not.

Below is my code

function includes(k) {
 for (var i = 0; i < this.length; i++) {
 if (this[i] === k || (this[i] !== this[i] && k !== k)) {
 return true;
 }
 }
 return false;
}
var dayValue = this.ui.dayConstraintList.val();
var arr = [courseTemplate.get('dayConstraints')[dayValue]];
console.log(arr);
arr.includes = includes;
console.log(arr.includes('roomTypeFilter'));

The first console.log returns an Object inside an array.

Object inside Array

The second console.log returns false, in this case as roomTypeFilter exists inside the Object I want to return 'true' but I am unsure how to do so, any help would be greatly appreciated.

asked Aug 17, 2016 at 13:07
3
  • I think you're looking for hasOwnProperty Commented Aug 17, 2016 at 13:12
  • 1
    jsfiddle.net/btjakgoo Commented Aug 17, 2016 at 13:16
  • console.log(!arr.filter(d => d.hasOwnProperty('roomTypeFilter')).length); Commented Aug 17, 2016 at 13:17

3 Answers 3

1

Instead of using includes, use hasOwnProperty. Take a look here for more information on the hasOwnProperty. It's pretty self-explanatory from its name - it essentially returns a boolean on whether or not the object has a property. I.e., in your case, you would use:

arr[0].hasOwnProperty('roomTypeFilter');
answered Aug 17, 2016 at 13:17
4
  • When I try console.log(arr[0].hasOwnProperty('roomTypeFilter')); I get Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined Commented Aug 17, 2016 at 13:25
  • 1
    Then your arr[0] must be undefined. Whichever object you would like check if it contains the function you're looking for, place that at arr[0]. The syntax is essentially object.hasOwnProperty('function/property') Commented Aug 17, 2016 at 13:27
  • Ahh yes, the function this is within is called on two different circumstances, it's initially undefined, but defined in another circumstance. Commented Aug 17, 2016 at 13:31
  • Great :) Glad i could help, if you have any further questions, let me know Commented Aug 17, 2016 at 13:37
1

You can use hasOwnProperty method to check if object contains roomTypeFilter property.

...
if (this[i].hasOwnProperty(k)) {
 ...
}
...
answered Aug 17, 2016 at 13:21
1

You could refactor your includes function to use

array.prototype.some

some() executes the callback function once for each element present in the array until it finds one where callback returns a truthy value... If such an element is found, some() immediately returns true.

Here is an example.

var arr = [
 {
 roomTypeFilter: {
 name: 'foo'
 }
 },
 ["roomTypeFilter", "foo"],
 "roomTypeFilter foo"
]
function includes(arr, k) {
 return arr.some(function(item) {
 return item === Object(item) && item.hasOwnProperty(k);
 })
}
console.log("Does arr includes roomTypeFilter ? - ", includes(arr, 'roomTypeFilter'))

answered Aug 17, 2016 at 13:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.