Validating if object data is "true" and retrieving relevant data if so - javascript

I have a problem to solve using some data from an object. The data could take a few forms and may or may not exist in the first place. For example
things : {
oranges: true,
apples : false
}
but it could equally be:
things : {
oranges: false,
apples : false
}
or maybe things doesn't even exist
I need to:
1) Determine that things exists
2) Determine that things contains further keys
These two statements need to be verified in one callable function e.g thingsHasData()
3) If things does have data, is any of the data set to true?
This also needs to be a callable function e.g fruitsIsTrue()
4) Return the key for one of the true values
trueFruit() - this should only return one key, but it doesn't matter which (it shouldn't ever have two true values as per business rules but it's more of a fallback to just return one if for some reason it does)
So I've been able to get the key of a true key-value pair using the following:
var thingsList = {
things : {
oranges: false,
apples : true
}
}
var trueFruit = Object.keys(thingsList).filter(function(key) {
return thingsList[key];
});
return thingsList[0];
This correctly returns apples and only apples so it works for point 4 but not the others, and I feel like there is a better way to do this not having to rely on repeating the same .filter in a few different functions. Ideas?

You could take functions and for a true value, use Array#find.
function thingsHasData(object) {
return 'things' in object;
}
function fruitsIsTrue(object) {
return 'things' in object && Object.values(object.things).some(Boolean);
}
function trueFruit(object) {
return 'things' in object && Object.keys(object.things).find(k => object.things[k]);
}
var a = {},
b = { things: {} },
c = { things: { oranges: true, apples : false } },
d = { things: { oranges: false, apples : false } };
[a, b, c, d].forEach(o => console.log(
thingsHasData(o),
fruitsIsTrue(o),
trueFruit(o)
));

To check if the Object things exist, you can use the following code:
if (typeof things != "undefined") {
// It exists!
}
To check if an object has any children, check Object.keys(things).length > 0.
So the check for 1) and 2) would look like:
let things = {
oranges: true,
apples: false
}
if (typeof things != "undefined") {
// It exists!
if (Object.keys(things).length > 0) {
// It has children!
}
}

var thingsList = {
things : {
oranges: false,
apples : true
},
things2 : {
oranges: true,
apples : true
}
};
function validateThings(things) {
// (1) checks for a falsy value of things
if (!things) {
return false;
}
var keys = Object.keys(things);
// (2) checks if things has keys
if (!keys.length) {
return false;
}
// (3) then it checks for every single keys value if it is truthy
for (var i = 0, len = keys.length; i < len; i++ ) {
if (things[keys[i]]) {
// (4) return this value — all tests passed
return things[keys[i]];
}
}
return false;
}
console.log(validateThings(thingsList.notInList));
console.log(validateThings(thingsList.things));
console.log(validateThings(thingsList.things2));

const thingsHasData = arg => (arg.things && Object.keys(arg.things).length>0) ? true : false;
const trueFruit = arg => {
if (!arg.things) return;
let fruitIndex = null;
let fruitValues = Object.values(arg.things);
fruitValues.forEach((value, index) => {
if (value) fruitIndex = Object.keys(arg.things)[index];
});
return fruitIndex;
}

Related

Multiple If Else Statements, only first is run

I have code that requires multiple If Else statements but I'm not sure how to format it so that each runs:
let example = first;
let example2 = second;
let example3 = third;
if (example === something) {
return null;
} else {
return something;
}
if (example2 === somethingElse) {
return null;
} else {
return somethingElse;
}
if (example3 === somethingMore) {
return null;
} else {
return somethingMore;
}
But this doesn't work because of the multiple else statements, I was wondering if there was a way to do this? I also tried to put the data into an array or objects to iterate through but that won't work either.
Please help! :)
return will immediate return from first if, so store all result in object or array and return it as below
let example = 'first';
let example2 = 'second';
let example3 = 'third';
var return_data = {};
if (example === 'something') {
return_data.example = null;
} else {
return_data.example = something;
}
if (example2 === 'somethingElse') {
return_data.example2 = null;
} else {
return_data.example2 = 'somethingElse';
}
if (example3 === 'somethingMore') {
return_data.example3 = null;
} else {
return_data.example3 = 'somethingMore';
}
return return_data;
You have to remove the return in the if / else blocks - using return will immediately exit the function wherever it's encountered. The way your code is now, you are basically short-circuiting the function (which is not what you're trying to do):
It would probably make more sense to restructure your code to use a variable like this:
//Add a variable to keep store your desired output if you want to flow thru all if/else blocks
function getVal(example) {
let val;
if (example === 'something1') {
val = 'a'
} else {
val = 'b';
}
return val;
}
console.log(getVal('something1'));
console.log(getVal('lorem'));
I'm not completely clear on what you are asking, but I think you want to be using "else if" statements: https://ncoughlin.com/javascript-notes-conditional-statements-loops/#If_Else_If_Else
let example = first;
let example2 = second;
let example3 = third;
if (example === something) {
return a;
} else if (example2 === somethingElse){
return b;
} else if (example3 === anotherThing){
return c;
} else {
return null;
}
You can do something like this :
myArray = [];
let example = first;
let example2 = second;
let example3 = third;
if (example === something) {
myArray.push(null);
} else {
myArray.(something);
}
if (example2 === somethingElse) {
myArray.push(null);
} else {
myArray.(somethingElse);
}
if (example3 === somethingMore) {
myArray.push(null);
} else {
myArray.(somethingMore);
}
return myArray;
Like Tom O. said return will immediatly exit your function. You can use something other than an array but remember return is executed only once.
Regardless of your approach, it seems like you want to build a "collection" of some sort (array, object, set, map, etc) then return it at the end.
But, the way you code it depends on the reason your function exists. Let's look at an example...
if (first === undefined) {
return null
} else {
return first
}
...This logic exists solely to ensure a "default" value is used for first - something like the null object pattern. For this use case, I might propose nullish coalescing to keep it simple (or something that could be easily replaced with it in the future):
first ?? null
// or, if you don't use babel/some kind of transpiler, you could want:
first !== undefined && first !== null ? first : null
// and since our default is null anyway, we can shorten this to:
first !== undefined ? first : null
Looking solely at your example, it seems like you could simply want to get default values like this for multiple variables. For that use case, you (or someone else coming across this question) might want a function similar to one in the code snippets below. Using objects and/or arrays for this can be handy because they can also be easily broken back out into multiple variables, if you wanted.
First, example functions using arrays:
// If you want default values for items in an array (static, all same default value)
const buildArrayWithDefault = (vals, defaultVal = null) => vals.map(
v => v !== undefined ? v : defaultVal // could be v ?? defaultVal
)
// If you want default values for items in an array (static, but defaults could all be different)
const buildArrayWithDefaults = (vals, defaultVals) => vals.map(
(v, idx) => v !== undefined ? v : defaultVals[idx] // could be v ?? defaultVals[idx]
)
// If you want default values for items in an array (dynamic via callback)
const buildArrayWithDefaults2 = (vals, getDefaultValue) => vals.map(
(v, idx) => v !== undefined ? v : getDefaultValue(v, idx)
)
// All of these return [ 1, 5, 3 ]
console.log(
buildArrayWithDefault([1, undefined, 3], 5),
buildArrayWithDefaults([1, undefined, 3], [ 4, 5, 6 ]),
buildArrayWithDefaults2([1, undefined, 3], (v, idx) => idx + 4)
)
Next, examples using objects:
// Hard-coded default values for an object (ternary)
const buildObject = (first, second, third) => ({
first: first !== undefined ? first : null, // or first ?? null
second: second !== undefined ? second : null,
third: third !== undefined ? third : null,
})
// Hard-coded default values for an object (default parameters)
const buildObject2 = (
first = null,
second = null,
third = null
) => (
{ first, second, third }
)
// ...or you can just use Object.assign()
const assignDefaults = (obj) => Object.assign(
{ first: null, second: null, third: null }, // defaults
obj
)
// Finally, allowing the function user to define their own defaults
// (At this point, you may just want to use Object.assign() directly)
const assignDefaults2 = (...args) => Object.assign({}, ...args.reverse())
// All of these should return { first: 1, second: null, third: null }
console.log(
buildObject(1),
buildObject2(1),
assignDefaults({ first: 1 }),
assignDefaults2({ first: 1 }, { first: null, second: null, third: null })
)

How to separate rules array and execute functions from that array

I have a class for validation which have different validation functions.
Now what i am trying to do is make an object in another file and send all the validation data using a constructor.This constructor will receive an object that looks like this "rules[is_empty:value]". In this left part is function name and value is the value fetched from input field.Now how do I call the function and send the value as an argument.Also what should i do when there are functions that has more than one argument.
I have already tried using map method and split method but not able to access the function.
class Validator {
constructor(rules) {
let rule_obj = {}
// rules[is_empty:value,has_valid_min_length:2;3]
}
/**this is to check if the field has an empty value or not */
is_empty = (value) => {
if (value == '' || value == null) {
return true
}
else {
return false
}
}
/**this is to check if value has required minimum length or not */
has_valid_min_length = (value, min_length = 0) => {
if (this.is_empty(value)) {
return false
}
if (value.length < min_length) {
return false
}
else {
return true
}
}
/**this is to check if value has more characters than maximum length */
has_valid_max_length = (value, max_length = 0) => {
if (this.is_empty(value)) {
return false
}
if (value.length > max_length) {
return false
}
else {
return true
}
}
//this is to check if selected date is less than given limit
is_before_min_date = (value_date, min_date) => {
if (this.is_empty(value)) {
return false
}
if (value_date < min_date) {
return true
}
else { return false }
}
//this is to check if selected date is higher than given limit
is_after_max_date = (value_date, max_date) => {
if (this.is_empty(value)) {
return false
}
if (value_date > max_date) {
return true
}
else {
return false
}
}
}
I want to call the function which is before ':' sign in the array and give that function argument which is in value that is at the right side of ':'.
Please help.
You could send an object through the constructor...
x = {
"is_empty": [0, 2, 2],
"has_valid_min_length": [ [value, min_length], [value, min_length] ],
"has_valid_max_length": [ [value, max_length], [value, max_length] ],
"is_before_min_date": [ [value_date, min_date], [value_date, min_date] ],
"is_after_max_date": [ [value_date, max_date], [value_date, max_date] ]
}
and then in your constructor, set up a loop through the object and value arrays...
constructor(to_be_validated) {
let validator_names = Object.keys(to_be_validated);
for (let validator of validator_names) {
let values = to_be_validated[validator];
if (validator === "is_empty") {
for (let value of values) {
this.is_empty(value);
}
} else if (validator === "has_valid_min_length") {
for (let value of values) {
this.has_valid_min_length(value[0], value[1]);
}
} etc...
}
}
and then when you call the function, the methods should execute
let my_validator = new Validator(x);
I echo the comment(s) above. Wanting the approach and specific syntax of,
let my_validator = new Validator(rules[validator_name:values]);
all in one clean line like that, is a bit off. I've never seen it done like that.
You probably want an additional function in your class that calls all the check-functions according to your rules.
class Validator {
constructor(rules) {
this.rules = rules;
// rules={is_empty:[],has_valid_min_length:[2]};
}
validate = (value) => {
let pass = true;
Object.keys(this.rules).forEach(k=>pass = pass && this[k](value, ...this.rules[k]));
return pass;
}
...
}
The rules-Objects has key-value-pairs, where the keys are the function-names of the individual checks and the values are arrays of parameters that will be passed. This array can have zero or more entries.
Edit: forEach will iterate over all the keys. On every turn k will hold the current key. pass is a boolean that collects all the return values of your checker-functions. (true && false === false) That way the final return-value will be false if any of the checks returned false.
You would then apply the rules to your data like this:
myValidatorObj.validate(data);

How to compare values of two objects after an API call, check for nulls or empties

I have an original object that is modified after an API call. I need to make sure that any fields that were originally not empty are reassigned to their original value. For example if articleTitle was initially filled out, and then after the API call it gets replaced with an empty value, I want to reassign it back to the original articleTitle value from the old object.
The two objects have the same keys, but I can't assume that the data coming back from the response is always going to be valid (but the original object always has valid data, that's why I need to reassign any empty fields to original values).
I (kinda) have a theoretically functional method, however I'm wondering if there is a more efficient way to do this. Here's what I have:
function evaluateEmptyValues = (originalReference, reference) {
// Get keys of both reference objects
var newReference = Object.entries(reference);
var oldReference = Object.entries(originalReference);
// Get length of both reference objects
var newReferenceLength = newReference.length;
var oldReferenceLength = oldReference.length;
// Double check objects are of the same length -- they always should be
if (newReferenceLength == oldReferenceLength) {
// Cycle through both objects
for (var i = 0; i < newReference.length; i++) {
console.log('i is ' + i);
// Again, these two lengths should be equal
if (newReference[i].length == oldReference[i].length) {
// Check if elements in current iteration is an object --
// if one is an object, then the other SHOULD also be
if ((typeof(newReference[i][j]) == 'object' &&
typeof(oldReference[i][j]) == 'object'
) {
// If both are objects, repeat lines 3 and 4
var currentNewReference = Object.entries(newReference[i][j]);
var currentOldReference = Object.entries(oldReference[i][j]);
// Get their lengths
var currentNewReferenceLength = currentNewReference.length;
var currentOldReferenceLength = currentOldReference.length;
// Both should be of the same length
if (currentNewReferenceLength == currentOldReferenceLength) {
for (var io = 0; io < currentNewReferenceLength.length; io++) {
console.log('io is ' + io);
// Both should also be of the same length
if (currentNewReference[io].length == currentOldReference[io].length) {
// For each iteration...
for (var jo = 0; jo < currentNewReference[io].length; jo++) {
// Check for empty values
if (currentNewReference[io][jo] == undefined ||
currentNewReference[io][jo] == null ||
(typeof(currentNewReference[io][jo]) == 'string' && currentNewReference[io][jo].trim() == '')
) {
// If empty, then reassign the empty value in the new reference
// object with the value of the field from the old reference
// object, regardless of whether or not the old value is also empty/null
currentNewReference[io][jo] = currentOldReference[io][jo];
}
}
} else {
// Serious problem
}
}
} else {
// Serious problem
}
} else {
// Cycle through current field
for (var j = 0; j < newReference[i].length; j++) {
// Check for nulls or empties
if (newReference[i][j] == undefined ||
newReference[i][j] == null ||
(typeof(newReference[i][j]) == 'string' && newReference[i][j].trim() == '')
) {
// Assign old value to new value, regardless of
// whether or not old value is also empty
newReference[i][j] = oldReference[i][j];
}
}
}
} else {
// Serious problem
}
}
} else {
// Serious problem
}
I doubt this is a very scalable or maintainable approach, and I'm wondering if there are any suggestions on enhancing this function, preferably using ES5, unless the ES6+ version works in most browsers.
For some reference, here are the two objects:
Here, articleTitle is empty.
Here, it is filled out from the API call. This is expected and needed, however imagine if it was the other way around, and articleTitle came back empty in the newReference after the API call
Edit:
Using the accepted answer plus an adjustment, this solved my specific problem:
function evaluateEmptyValues(reference, originalReference) {
var vm = this;
// Get keys and values of both reference objects
referenceLength = Object.entries(reference).length;
originalReferenceLength = Object.entries(originalReference).length;
if (referenceLength == originalReferenceLength) {
try {
// Cycle through both objects
for (var prop in reference) {
if (reference[prop] != undefined || reference[prop] != null) {
if (typeof (reference[prop]) == 'string' && reference[prop].trim() != '') {
// If both current elements are objects, recurse
if (typeof reference[prop] == 'object' && typeof originalReference[prop] == 'object') {
vm.evaluateEmptyValues(reference[prop], originalReference[prop])
}
// If both current elements are arrays, recurse
if (Array.isArray(reference[prop]) && typeof Array.isArray(originalReference[prop])) {
reference[prop].forEach((item, index) => vm.evaluateEmptyValues(item, originalReference[prop][index]));
}
// If new value is null, empty or undefined, assign it to old value,
// regardless of whether or not the old value was also null/empty.
//
///// This is to ensure that no existing previous values are
///// overwritten with any nulls or empty values
} else {
reference[prop] = originalReference[prop];
}
} else {
reference[prop] = originalReference[prop];
}
}
} catch(err) {
console.log(err);
}
}
console.log(reference);
You can simplify your function by a lot using recursion and a for ... in loop. I made two test objects to illustrate all the cases of your original example. In case it hits an array of objects it will iterate through that array and check for empty values recursively as well. Please see snippet below:
function evaluateEmptyValues(reference, originalReference) {
if (reference.length == originalReference.length) {
for (var prop in reference) {
if (typeof reference[prop] == 'object' && typeof originalReference[prop] == 'object') {
evaluateEmptyValues(reference[prop], originalReference[prop])
}
if (Array.isArray(reference[prop]) && typeof Array.isArray(originalReference[prop])) {
reference[prop].forEach((item, index) => evaluateEmptyValues(item, originalReference[prop][index]));
}
if (reference[prop] == undefined || reference[prop] == null ||
(typeof (reference[prop]) == 'string' && reference[prop].trim() == '')) {
reference[prop] = originalReference[prop];
}
}
}
}
const original = {
name: "Jack",
employee: {
firstName: "Nathan",
favoriteAnimal: {
species: "Donkey",
nature: "Lazy"
},
favoriteBeverages: [
{ name: "Beer", temperature: "Cold" },
{ name: "More beer", temperature: "Colder" }
]
},
occupation: "Plumber"
}
const newObject = {
name: "Jack",
employee: {
firstName: " ",
favoriteAnimal: {
species: null,
nature: "Lazy"
},
favoriteBeverages: [
{ name: "Beer", temperature: ""},
{ name: null, temperature: "Colder" }
]
},
occupation: undefined
}
evaluateEmptyValues(newObject, original);
console.log(newObject);
I think instead of using lot's of if conditions, you can try lodash, and use isEqual method which do a deep comparison between two values ( in your case two objects ), your code can be much cleaner as well.
var object = { 'a': 1 };
var other = { 'a': 1 };
_.isEqual(object, other);
// => true
You could make use of a recursive function. Something like this.
function mapper(oldObj, newObj) {
Object.entries(oldObj).forEach(([key, value]) => {
if (!newObj[key]) {
newObj[key] = value;
} else if (Array.isArray(newObj[key])) {
newObj[key].forEach((o, i) => mapper(oldObj[key][i], o));
} else if (Object.prototype.toString.call(newObj[key]) === "[object Object]") {
mapper(oldObj[key], newObj[key]);
}
});
return newObj;
}
const next = mapper(oldObj, newObj);
This will basically loop over all the items in the original object, and set the key/value in the new object if it doesn't exist.

Efficient way to find first occurrence of key:value inside object

Below is the object in which I need to find at least one occurrence of isSelected: true.
[
{
"isSelected": true,
"child": [
{
"isSelected": true,
"child": [
{
"isSelected": true,
"child": [
{
"isSelected": true
}
]
}
]
}
]
}
]
The above object can have n elements in it and each element can have n children and so on. For every element there will be an isSelected key with value "true/false".
I am trying to write a function in JavaScript that will return true if it finds at least one occurrence of isSelected key with true value.
Wrote below function using JSON.stringify() and search for string "isSelected:true" string in it
function hasIsSelected(data){
return (JSON.stringify(data)).search('"isSelected":true') > -1 ? true: false
}
Not sure if JSON.stringify() will be efficient for large objects.
Trying to find solution in JavaScript without using third party library.
You can use a recursive algorithm to check the "isSelected" value and loop over all the children :
function hasIsSelected(data) {
if (data.isSelected) {
return true;
}
if (data.child) {
for (var i = 0; i < data.child.length; i++) {
if (hasIsSelected(data.child[i])) {
return true;
}
}
}
return false;
}
var json = [...]; // Your value
hasIsSelected(json[0]);
EDIT :
Ok let's make a very simple benchmark for the worst case :
function createTestData(depth) {
var obj = { isSelected: depth === 0 };
if (depth > 0) {
obj.child = [createTestData(depth - 1)];
}
return obj;
}
var testData = [createTestData(1000)]; // Big object, the "true" value is in the deepest child.
function hasIsSelectedStrinfigy(data){
return (JSON.stringify(data)).search('"isSelected":true') > -1;
}
function hasIsSelectedRec(data) {
if (data.isSelected) {
return true;
}
if (data.child) {
for (var i = 0; i < data.child.length; i++) {
if (hasIsSelectedRec(data.child[i])) {
return true;
}
}
}
return false;
}
// Using NicolaeS's solution
function hasIsSelectedRecTOC(data) {
if (data.isSelected === true) {
return true;
}
if (data.child instanceof Array) {
// stops after the first valid element
return data.child.some(hasIsSelectedRecTOC);
}
return false;
}
// Run tests
function runTest(fun) {
var t0 = performance.now();
fun(testData[0]);
var t1 = performance.now();
return t1 - t0;
}
console.log("Exec time using stringify : %o", runTest(hasIsSelectedStrinfigy));
console.log("Exec time using recursion : %o", runTest(hasIsSelectedRec));
console.log("Exec time using recursion with TOC : %o", runTest(hasIsSelectedRecTOC));
Results on my computer (change every time you run them but you get the idea) :
Exec time using stringify : 6.785000000000004
Exec time using recursion : 0.36999999999999034
Exec time using recursion with TOC : 0.37999999999999545
This was for the worst case. Now with the best case (the first isSelected is "true") :
function createTestData(depth) {
var obj = { isSelected: true }; // isSelected is always true
if (depth > 0) {
obj.child = [createTestData(depth - 1)];
}
return obj;
}
var testData = [createTestData(1000)];
Results :
Exec time using stringify : 3.980000000000002
Exec time using recursion : 0.040000000000000924
Exec time using recursion with TOC : 0.02499999999999858
Building on the answer of #Junior - recursion is the fastest way to do it, but here is a more performant version using tail call optimization:
function hasIsSelected(data) {
if (data.isSelected === true) {
return true;
} else if (data.child instanceof Array) {
return data.child.some(hasIsSelected); // stops after the first selected element
} else return false;
}
Another important trick is to stop the loop as soon as a true is found.
Recursion will be the best way to do that:
const deepSearch = (arr) => {
return arr.some((v) => {
if (v.isSelected === true) {
return true;
}
if (Array.isArray(v.child) && v.child.length > 0) {
return deepSearch(v.child);
}
return false;
});
};
Here is jsperf test.
Added: Array.isArray(X) is ≈3.3 times faster than X instanceof Array. Here is jsperf test confirming that.

Object has-property-deep check in JavaScript

Let's say we have this JavaScript object:
var object = {
innerObject:{
deepObject:{
value:'Here am I'
}
}
};
How can we check if value property exists?
I can see only two ways:
First one:
if(object && object.innerObject && object.innerObject.deepObject && object.innerObject.deepObject.value) {
console.log('We found it!');
}
Second one:
if(object.hasOwnProperty('innerObject') && object.innerObject.hasOwnProperty('deepObject') && object.innerObject.deepObject.hasOwnProperty('value')) {
console.log('We found it too!');
}
But is there a way to do a deep check? Let's say, something like:
object['innerObject.deepObject.value']
or
object.hasOwnProperty('innerObject.deepObject.value')
There isn't a built-in way for this kind of check, but you can implement it easily. Create a function, pass a string representing the property path, split the path by ., and iterate over this path:
Object.prototype.hasOwnNestedProperty = function(propertyPath) {
if (!propertyPath)
return false;
var properties = propertyPath.split('.');
var obj = this;
for (var i = 0; i < properties.length; i++) {
var prop = properties[i];
if (!obj || !obj.hasOwnProperty(prop)) {
return false;
} else {
obj = obj[prop];
}
}
return true;
};
// Usage:
var obj = {
innerObject: {
deepObject: {
value: 'Here am I'
}
}
}
console.log(obj.hasOwnNestedProperty('innerObject.deepObject.value'));
You could make a recursive method to do this.
The method would iterate (recursively) on all 'object' properties of the object you pass in and return true as soon as it finds one that contains the property you pass in. If no object contains such property, it returns false.
var obj = {
innerObject: {
deepObject: {
value: 'Here am I'
}
}
};
function hasOwnDeepProperty(obj, prop) {
if (typeof obj === 'object' && obj !== null) { // only performs property checks on objects (taking care of the corner case for null as well)
if (obj.hasOwnProperty(prop)) { // if this object already contains the property, we are done
return true;
}
for (var p in obj) { // otherwise iterate on all the properties of this object.
if (obj.hasOwnProperty(p) && // and as soon as you find the property you are looking for, return true
hasOwnDeepProperty(obj[p], prop)) {
return true;
}
}
}
return false;
}
console.log(hasOwnDeepProperty(obj, 'value')); // true
console.log(hasOwnDeepProperty(obj, 'another')); // false
Alternative recursive function:
Loops over all object keys. For any key it checks if it is an object, and if so, calls itself recursively.
Otherwise, it returns an array with true, false, false for any key with the name propName.
The .reduce then rolls up the array through an or statement.
function deepCheck(obj,propName) {
if obj.hasOwnProperty(propName) { // Performance improvement (thanks to #nem's solution)
return true;
}
return Object.keys(obj) // Turns keys of object into array of strings
.map(prop => { // Loop over the array
if (typeof obj[prop] == 'object') { // If property is object,
return deepCheck(obj[prop],propName); // call recursively
} else {
return (prop == propName); // Return true or false
}
}) // The result is an array like [false, false, true, false]
.reduce(function(previousValue, currentValue, index, array) {
return previousValue || currentValue;
} // Do an 'or', or comparison of everything in the array.
// It returns true if at least one value is true.
)
}
deepCheck(object,'value'); // === true
PS: nem035's answer showed how it could be more performant: his solution breaks off at the first found 'value.'
My approach would be using try/catch blocks. Because I don't like to pass deep property paths in strings. I'm a lazy guy who likes autocompletion :)
JavaScript objects are evaluated on runtime. So if you return your object statement in a callback function, that statement is not going to be evaluated until callback function is invoked.
So this function just wraps the callback function inside a try catch statement. If it catches the exception returns false.
var obj = {
innerObject: {
deepObject: {
value: 'Here am I'
}
}
};
const validate = (cb) => {
try {
return cb();
} catch (e) {
return false;
}
}
if (validate(() => obj.innerObject.deepObject.value)) {
// Is going to work
}
if (validate(() => obj.x.y.z)) {
// Is not going to work
}
When it comes to performance, it's hard to say which approach is better.
On my tests if the object properties exist and the statement is successful I noticed using try/catch can be 2x 3x times faster than splitting string to keys and checking if keys exist in the object.
But if the property doesn't exist at some point, prototype approach returns the result almost 7x times faster.
See the test yourself: https://jsfiddle.net/yatki/382qoy13/2/
You can also check the library I wrote here: https://github.com/yatki/try-to-validate
I use try-catch:
var object = {
innerObject:{
deepObject:{
value:'Here am I'
}
}
};
var object2 = {
a: 10
}
let exist = false, exist2 = false;
try {
exist = !!object.innerObject.deepObject.value
exist2 = !!object2.innerObject.deepObject.value
}
catch(e) {
}
console.log(exist);
console.log(exist2);
Try this nice and easy solution:
public hasOwnDeepProperty(obj, path)
{
for (var i = 0, path = path.split('.'), len = path.length; i < len; i++)
{
obj = obj[path[i]];
if (!obj) return false;
};
return true;
}
In case you are writing JavaScript for Node.js, then there is an assert module with a 'deepEqual' method:
const assert = require('assert');
assert.deepEqual(testedObject, {
innerObject:{
deepObject:{
value:'Here am I'
}
}
});
I have created a very simple function for this using the recursive and happy flow coding strategy. It is also nice to add it to the Object.prototype (with enumerate:false!!) in order to have it available for all objects.
function objectHasOwnNestedProperty(obj, keys)
{
if (!obj || typeof obj !== 'object')
{
return false;
}
if(typeof keys === 'string')
{
keys = keys.split('.');
}
if(!Array.isArray(keys))
{
return false;
}
if(keys.length == 0)
{
return Object.keys(obj).length > 0;
}
var first_key = keys.shift();
if(!obj.hasOwnProperty(first_key))
{
return false;
}
if(keys.length == 0)
{
return true;
}
return objectHasOwnNestedProperty(obj[first_key],keys);
}
Object.defineProperty(Object.prototype, 'hasOwnNestedProperty',
{
value: function () { return objectHasOwnNestedProperty(this, ...arguments); },
enumerable: false
});

Categories

Resources