Check If 'Any' Value Is Object

Check if a value is an object in JavaScript

UPDATE:

This answer is incomplete and gives misleading results. For example, null is also considered of type object in JavaScript, not to mention several other edge cases. Follow the recommendation below and move on to other "most upvoted (and correct!) answer":

typeof yourVariable === 'object' && yourVariable !== null

Original answer:

Try using typeof(var) and/or var instanceof something.

EDIT: This answer gives an idea of how to examine variable's properties, but it is not a bulletproof recipe (after all there's no recipe at all!) for checking whether it's an object, far from it. Since people tend to look for something to copy from here without doing any research, I'd highly recommend that they turn to the other, most upvoted (and correct!) answer.

Check if `Any` value is object

UPDATE

The code I have shown below is reported as not working in release build.
(Please see Paul Cantrell's comment below.)

Apologies for my "as far as I tested" was too limited.

I'll update this answer when I find some further info about this.


I'm not sure we can see this behaviour in the next beta (or GM or Released version...), but this works as you expect in Xcode 8 beta 6.

let foo: Any = 4
if type(of: foo) is AnyClass {
print("It's an object.")
let object = foo as AnyObject
//do something with `object` that requires reference semantics
} else {
print("It's not an object.") //->It's not an object.
}

class MyClass {}
let bar: Any = MyClass()
if type(of: bar) is AnyClass {
print("It's an object.") //->It's an object.
let object = foo as AnyObject
//do something with `object` that requires reference semantics
} else {
print("It's not an object.")
}

let baz: Any = Array<AnyObject>()
if type(of: baz) is AnyClass {
print("It's an object.")
let object = foo as AnyObject
//do something with `object` that requires reference semantics
} else {
print("It's not an object.") //->It's not an object.
}

I cannot check all possible cases, so there may be some edge cases where this does not work. But as far as I tested, this seems to work as expected.

how to check if an object has at least one true value

Assuming that values is actually an object, check if .some of the Object.values of the object are true:

const values = {de: true, en: false, nl: false, pl: false, ru: false};
const someTruthy = Object.values(values).some(val => val === true);console.log(someTruthy);

Check if a value is an object in JavaScript

UPDATE:

This answer is incomplete and gives misleading results. For example, null is also considered of type object in JavaScript, not to mention several other edge cases. Follow the recommendation below and move on to other "most upvoted (and correct!) answer":

typeof yourVariable === 'object' && yourVariable !== null

Original answer:

Try using typeof(var) and/or var instanceof something.

EDIT: This answer gives an idea of how to examine variable's properties, but it is not a bulletproof recipe (after all there's no recipe at all!) for checking whether it's an object, far from it. Since people tend to look for something to copy from here without doing any research, I'd highly recommend that they turn to the other, most upvoted (and correct!) answer.

How to detect if a variable is a pure javascript object

To achieve expected result, use below option of finding constructor name to check if variable is pure javascript Object or not

As per MDN,

All objects (with the exception of objects created with
Object.create(null)) will have a constructor property. Objects created
without the explicit use of a constructor function (i.e. the object
and array literals) will have a constructor property that points to
the Fundamental Object constructor type for that object.

Please refer this link for more details on constructor property - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor

var x = {a:1,b:2};
var y = [1,2,3];

console.log(x.constructor.name === "Object")//x.constructor.name is Object
console.log(y.constructor.name === "Object")//y.constructor.name is Array

check if a value is string in an object javascript

Here's an approach that might be easier to understand.

This code logs an error if one or more properties are not a string:

var obj = {  "val1": "test1",  "val2": 1,  "val3": null};
for (var property in obj) { if (typeof obj[property] !== 'string') { console.error(property + ' is not a string!'); }}

Check if any part of an object value is included in string

You can get rid any / with replace method

let oldstr = '/link-to-page?foo=bar&test=1';
let str = oldstr.replace('/', '');
console.log(str)

let obj = {
key: '/',
foo: 'asd',
test: false,
mock: 'data'
}

let a = Object.values(obj).some(s => str.includes(s))
console.log(a);

how to check if all object keys has false values

This will do the trick...

var result = true;

for (var i in saver) {
if (saver[i] === true) {
result = false;
break;
}
}

You can iterate objects using a loop, either by index or key (as above).

If you're after tidy code, and not repeating that then simply put it in a function...

Object.prototype.allFalse = function() { 
for (var i in this) {
if (this[i] === true) return false;
}
return true;
}

Then you can call it whenever you need, like this...

alert(saver.allFalse());

Here's a working sample...

Object.prototype.allFalse = function() {     for (var i in this) {        if (this[i] === true) return false;    }    return true;}
var saver = { title: false, preview: false, body: false, bottom: false, locale: false};
console.log("all are false - should alert 'true'");console.log(saver.allFalse());
saver.body = true;
console.log("one is now true - should alert 'false'");console.log(saver.allFalse());

Check if object value exists within a Javascript array of objects and if not add a new object to array

I've assumed that ids are meant to be unique here. some is a great function for checking the existence of things in arrays: