Single line subobject check - javascript

I'm working with two objects in JS and I need to know if a given subobject exists. My objects look something like this:
obj1 = {
sobj1: 1234,
sobj2: {
ssobj1: "abc",
ssobj2: 1234
}
}
The catch is that I don't know beforehand the exact shape of the object and when I try to check if ssojb1 exists with an if (obj1.sobj2.ssobj1) and sobj2 isn't set, I'll get an error like "trying to read property ssobj1 of undefined.
To circumvent this, my approach was to use a cascade of if statements, but I'm pretty sure that there's a better option. This can get ugly pretty quickly.
Any suggestions?

There is an optional chaining operator supported in modern browsers ?., but you may still be working with if statements at that point. In these cases, utilities may help you.
const isDefined = x => typeof x !== 'undefined'
const { get } = rubico
const pathsToCheck = [
'sobj2.ssobj1',
'sobj2.ssobj3',
]
const checkPath = (path, obj) => isDefined(get(path)(obj))
const obj1 = {
sobj1: 1234,
sobj2: {
ssobj1: "abc",
ssobj2: 1234
}
}
for (const path of pathsToCheck) {
console.log(path, checkPath(path, obj1))
}
<script src="https://unpkg.com/rubico"></script>
Disclaimer: I am the author of rubico.
Documentation for get

Thanks to mousetail for the solution.
Appearently using if ('key' in obj) does the trick, even if obj is undefined.
So:
if ('ssobj1' in obj1) { // true or false
do stuff...
}

Related

why cant I access the object values within state when they are clearly shown? [duplicate]

In my code, I deal with an array that has some entries with many objects nested inside one another, where as some do not. It looks something like the following:
// where this array is hundreds of entries long, with a mix
// of the two examples given
var test = [{'a':{'b':{'c':"foo"}}}, {'a': "bar"}];
This is giving me problems because I need to iterate through the array at times, and the inconsistency is throwing me errors like so:
for (i=0; i<test.length; i++) {
// ok on i==0, but 'cannot read property of undefined' on i==1
console.log(a.b.c);
}
I am aware that I can say if(a.b){ console.log(a.b.c)}, but this is extraordinarily tedious in cases where there are up to 5 or 6 objects nested within one another. Is there any other (easier) way that I can have it ONLY do the console.log if it exists, but without throwing an error?
Update:
If you use JavaScript according to ECMAScript 2020 or later, see optional chaining.
TypeScript has added support for optional chaining in version 3.7.
// use it like this
obj?.a?.lot?.of?.properties
Solution for JavaScript before ECMASCript 2020 or TypeScript older than version 3.7:
A quick workaround is using a try/catch helper function with ES6 arrow function:
function getSafe(fn, defaultVal) {
try {
return fn();
} catch (e) {
return defaultVal;
}
}
// use it like this
console.log(getSafe(() => obj.a.lot.of.properties));
// or add an optional default value
console.log(getSafe(() => obj.a.lot.of.properties, 'nothing'));
What you are doing raises an exception (and rightfully so).
You can always do:
try{
window.a.b.c
}catch(e){
console.log("YO",e)
}
But I wouldn't, instead think of your use case.
Why are you accessing data, 6 levels nested that you are unfamiliar of? What use case justifies this?
Usually, you'd like to actually validate what sort of object you're dealing with.
Also, on a side note you should not use statements like if(a.b) because it will return false if a.b is 0 or even if it is "0". Instead check if a.b !== undefined
If I am understanding your question correctly, you want the safest way to determine if an object contains a property.
The easiest way is to use the in operator.
window.a = "aString";
//window should have 'a' property
//lets test if it exists
if ("a" in window){
//true
}
if ("b" in window){
//false
}
Of course you can nest this as deep as you want
if ("a" in window.b.c) { }
Not sure if this helps.
Try this. If a.b is undefined, it will leave the if statement without any exception.
if (a.b && a.b.c) {
console.log(a.b.c);
}
If you are using lodash, you could use their has function. It is similar to the native "in", but allows paths.
var testObject = {a: {b: {c: 'walrus'}}};
if(_.has(testObject, 'a.b.c')) {
//Safely access your walrus here
}
If you use Babel, you can already use the optional chaining syntax with #babel/plugin-proposal-optional-chaining Babel plugin. This would allow you to replace this:
console.log(a && a.b && a.b.c);
with this:
console.log(a?.b?.c);
If you have lodash you can use its .get method
_.get(a, 'b.c.d.e')
or give it a default value
_.get(a, 'b.c.d.e', default)
I use undefsafe religiously. It tests each level down into your object until it either gets the value you asked for, or it returns "undefined". But never errors.
This is a common issue when working with deep or complex json object, so I try to avoid try/catch or embedding multiple checks which would make the code unreadable, I usually use this little piece of code in all my procect to do the job.
/* ex: getProperty(myObj,'aze.xyz',0) // return myObj.aze.xyz safely
* accepts array for property names:
* getProperty(myObj,['aze','xyz'],{value: null})
*/
function getProperty(obj, props, defaultValue) {
var res, isvoid = function(x){return typeof x === "undefined" || x === null;}
if(!isvoid(obj)){
if(isvoid(props)) props = [];
if(typeof props === "string") props = props.trim().split(".");
if(props.constructor === Array){
res = props.length>1 ? getProperty(obj[props.shift()],props,defaultValue) : obj[props[0]];
}
}
return typeof res === "undefined" ? defaultValue: res;
}
I like Cao Shouguang's answer, but I am not fond of passing a function as parameter into the getSafe function each time I do the call. I have modified the getSafe function to accept simple parameters and pure ES5.
/**
* Safely get object properties.
* #param {*} prop The property of the object to retrieve
* #param {*} defaultVal The value returned if the property value does not exist
* #returns If property of object exists it is returned,
* else the default value is returned.
* #example
* var myObj = {a : {b : 'c'} };
* var value;
*
* value = getSafe(myObj.a.b,'No Value'); //returns c
* value = getSafe(myObj.a.x,'No Value'); //returns 'No Value'
*
* if (getSafe(myObj.a.x, false)){
* console.log('Found')
* } else {
* console.log('Not Found')
* }; //logs 'Not Found'
*
* if(value = getSafe(myObj.a.b, false)){
* console.log('New Value is', value); //logs 'New Value is c'
* }
*/
function getSafe(prop, defaultVal) {
return function(fn, defaultVal) {
try {
if (fn() === undefined) {
return defaultVal;
} else {
return fn();
}
} catch (e) {
return defaultVal;
}
}(function() {return prop}, defaultVal);
}
Lodash has a get method which allows for a default as an optional third parameter, as show below:
const myObject = {
has: 'some',
missing: {
vars: true
}
}
const path = 'missing.const.value';
const myValue = _.get(myObject, path, 'default');
console.log(myValue) // prints out default, which is specified above
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Imagine that we want to apply a series of functions to x if and only if x is non-null:
if (x !== null) x = a(x);
if (x !== null) x = b(x);
if (x !== null) x = c(x);
Now let's say that we need to do the same to y:
if (y !== null) y = a(y);
if (y !== null) y = b(y);
if (y !== null) y = c(y);
And the same to z:
if (z !== null) z = a(z);
if (z !== null) z = b(z);
if (z !== null) z = c(z);
As you can see without a proper abstraction, we'll end up duplicating code over and over again. Such an abstraction already exists: the Maybe monad.
The Maybe monad holds both a value and a computational context:
The monad keeps the value safe and applies functions to it.
The computational context is a null check before applying a function.
A naive implementation would look like this:
⚠️ This implementation is for illustration purpose only! This is not how it should be done and is wrong at many levels. However this should give you a better idea of what I am talking about.
As you can see nothing can break:
We apply a series of functions to our value
If at any point, the value becomes null (or undefined) we just don't apply any function anymore.
const abc = obj =>
Maybe
.of(obj)
.map(o => o.a)
.map(o => o.b)
.map(o => o.c)
.value;
const values = [
{},
{a: {}},
{a: {b: {}}},
{a: {b: {c: 42}}}
];
console.log(
values.map(abc)
);
<script>
function Maybe(x) {
this.value = x; //-> container for our value
}
Maybe.of = x => new Maybe(x);
Maybe.prototype.map = function (fn) {
if (this.value == null) { //-> computational context
return this;
}
return Maybe.of(fn(this.value));
};
</script>
Appendix 1
I cannot explain what monads are as this is not the purpose of this post and there are people out there better at this than I am. However as Eric Elliot said in hist blog post JavaScript Monads Made Simple:
Regardless of your skill level or understanding of category theory, using monads makes your code easier to work with. Failing to take advantage of monads may make your code harder to work with (e.g., callback hell, nested conditional branches, more verbosity).
Appendix 2
Here's how I'd solve your issue using the Maybe monad from monetjs
const prop = key => obj => Maybe.fromNull(obj[key]);
const abc = obj =>
Maybe
.fromNull(obj)
.flatMap(prop('a'))
.flatMap(prop('b'))
.flatMap(prop('c'))
.orSome('🌯')
const values = [
{},
{a: {}},
{a: {b: {}}},
{a: {b: {c: 42}}}
];
console.log(
values.map(abc)
);
<script src="https://www.unpkg.com/monet#0.9.0/dist/monet.js"></script>
<script>const {Maybe} = Monet;</script>
In str's answer, value 'undefined' will be returned instead of the set default value if the property is undefined. This sometimes can cause bugs. The following will make sure the defaultVal will always be returned when either the property or the object is undefined.
const temp = {};
console.log(getSafe(()=>temp.prop, '0'));
function getSafe(fn, defaultVal) {
try {
if (fn() === undefined || fn() === null) {
return defaultVal
} else {
return fn();
}
} catch (e) {
return defaultVal;
}
}
You can use optional chaining from the ECMAScript standart.
Like this:
a?.b?.c?.d?.func?.()
I answered this before and happened to be doing a similar check today. A simplification to check if a nested dotted property exists. You could modify this to return the value, or some default to accomplish your goal.
function containsProperty(instance, propertyName) {
// make an array of properties to walk through because propertyName can be nested
// ex "test.test2.test.test"
let walkArr = propertyName.indexOf('.') > 0 ? propertyName.split('.') : [propertyName];
// walk the tree - if any property does not exist then return false
for (let treeDepth = 0, maxDepth = walkArr.length; treeDepth < maxDepth; treeDepth++) {
// property does not exist
if (!Object.prototype.hasOwnProperty.call(instance, walkArr[treeDepth])) {
return false;
}
// does it exist - reassign the leaf
instance = instance[walkArr[treeDepth]];
}
// default
return true;
}
In your question you could do something like:
let test = [{'a':{'b':{'c':"foo"}}}, {'a': "bar"}];
containsProperty(test[0], 'a.b.c');
I usually use like this:
var x = object.any ? object.any.a : 'def';
You can avoid getting an error by giving a default value before getting the property
var test = [{'a':{'b':{'c':"foo"}}}, {'a': "bar"}];
for (i=0; i<test.length; i++) {
const obj = test[i]
// No error, just undefined, which is ok
console.log(((obj.a || {}).b || {}).c);
}
This works great with arrays too:
const entries = [{id: 1, name: 'Scarllet'}]
// Giving a default name when is empty
const name = (entries.find(v => v.id === 100) || []).name || 'no-name'
console.log(name)
Unrelated to the question's actual question, but might be useful for people coming to this question looking for answers.
Check your function parameters.
If you have a function like const x({ a }) => { }, and you call it without arguments x(); append = {} to the parameter: const x({ a } = {}) => { }.
What I had
I had a function like this:
const x = ({ a }) => console.log(a);
// This one works as expected
x({ a: 1 });
// This one errors out
x();
Which results in "Uncaught TypeError: Cannot destructure property 'a' of 'undefined' as it is undefined."
What I switched it to (now works).
const x = ({ a } = {}) => console.log(a);
// This one works as expected
x({ a: 1 });
// This now works too!
x();

Typescript array of arrays of arrays: protect from undefined

I am parsing a complex object in Typescript, so have something like:
const a = reply['price']['value']['total']['value'];
and I like to ensure that all elements are defined in the chain, otherwise, it should set a=0 and do not trigger an exception if some of the chained keys are in fact undefined.
What would be the appropriate syntax for that?
If you're using modern JS you can use nullish coalescing and optional chaining
const a = reply?.['price']?.['value']?.['total']?.['value'] ?? 0;
Try to avoid using || instead of ??, because that will give you 0, if the final value is any falsy value, like 0 or false.
If you don't want to use nullish coalescing, you can do this, which achieves the same.
const a = reply?.['price']?.['value']?.['total']?.['value'] ? reply['price']['value']['total']['value'] : 0;
If you can't use #nullptr 's answer, I suggest creating a function for that purpose :
function getObjectNestedValue2(obj, keys, defaultValue) {
let currObjOrValue = obj;
keys.forEach(key => {
if (!currObjOrValue.hasOwnProperty(key)) {
currObjOrValue = defaultValue;
return;
}
currObjOrValue = currObjOrValue[key];
});
return currObjOrValue;
}
You then call it like so :
const a = getObjectNestedValue(reply, ['price', 'value', 'total', 'value'], 0);
Cheers

ES6 spread element - default value

I need to set an empty object as a default value if the array I'm passing in is empty. Something like:
var obj = { documents: [...question.documents] || [{}] }
I fixed it using a condition, but I want to know if there is a better way to achieve that.
if(obj.documents.length === 0) obj.documents.push({})
Since even empty arrays are truthy, I don't think there's any great elegant solution other than putting an explicit test in there somewhere. Ternaries are more terse than if statements, though:
const question = { documents: [] };
const { documents } = question;
const obj = { documents: documents.length !== 0 ? documents : [{}]}
console.log(JSON.stringify(obj));
Here's another possibility:
const question = { documents: [] };
const [firstElm = {}, ...otherElms] = question.documents;
const obj = { documents: [firstElm, ...otherElms] };
console.log(obj);
There are a couple of ways to write this in a single expression
Using the ternary operator:
var obj = { documents: [
...question.documents.length
? question.documents
: [{}]
]
};
Using a default value
var obj = { documents: [question.documents[0] || {}, ...question.documents.slice(1)] };
In both cases there's some awkwardness stemming from having to refer to the source multiple times
The spread operator is used inside an empty array. I don't see the
point in using the spread operator here. The objective can be achieved
by using the following.
var obj = { documents: question.documents.length ? question.documents : [{}]}
If the method you have provided is being used, you don't need an or clause, because an empty array also returns a truthy value. So it can be written as the following :-
var obj = { documents: question.documents }
if(!obj.documents.length) obj.documents.push({})
this should suit...
const question = {
documents: [],
};
const obj = {
documents: [].concat(question.documents.length ? question.documents : {})
};
console.log(obj);
The shortest way
const obj1 ={...(true&& {x:1})};
console.log(obj1)
const obj2 ={...(false&& {y:1})};
console.log(obj2)

Transforming array into a object in JavaScript

I am trying to convert an array into a javscript object that is designed to work with input checkboxes in AngularJS.
This is the input array that I get from my backend:
let selectedRolesDB = ['ADMIN', 'SECURITY'];
This is what my front-end expects:
let selectedRoles =
{
'ADMIN' : true,
'SECURITY': true
};
I tried different approaches such as angular.forEach but the problem is that I am not able to get the desired output:
angular.forEach(selectedRolesDB,(value, key) => {
this.selectedRoles.push({value : true });
});
Can anyone tell me how I best solve my problem so I end up with the array that my front-end expects?
JSFiddle
selectedRoles is not array, it is object. Init it as empty object:
let selectedRoles = {};
angular.forEach(selectedRolesDB,(value, key) => {
// use [] notation to add property with required name and value
selectedRoles[value] = true;
});
Use array.reduce :
let selectedRolesDB = ['ADMIN', 'SECURITY'];
const newArray = selectedRolesDB.reduce((accumulator, value) => {
accumulator[value] = true;
return accumulator;
}, {});
console.log(newArray)
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce for documentation about it.
Probably it would be much better to use array's native 'forEach' method (it has a good browser support, see here Array forEach). Also this will be helpful if you decide to migrate your project into Angular2+, so avoiding usages of 'angular.someMethod' is a better approach.
This is a final solution:
const selectedRoles: {[key: string]: boolean} = {};
selectedRolesDB.forEach((value: string) => selectedRoles[value] = true);

How to declare a nested Javascript object in one statement with variable keys

This is probably something that Ruby does better, but is there a better way to write this Javascript code (for React):
handleCellChange: function(rowIdx, prop, val) {
var updateObj = {};
updateObj.data = {};
updateObj.data[rowIdx] = {};
updateObj.data[rowIdx][prop] = { $set: val };
var newState = React.addons.update(this.state, updateObj);
In ES6 you can use computed property names:
updateObj = { data: { [rowIdx]: { [prop]: {$set: val} } } };
You can use this in Traceur or Firefox nightly, etc. (but not yet in node --harmony).
Here is a thing which will transpile this syntax for you: https://www.npmjs.org/package/es6-computed-property-keys.
For more information see http://wiki.ecmascript.org/doku.php?id=harmony:object_literals#object_literal_computed_property_keys. Also see "computed properties" item in http://kangax.github.io/compat-table/es6/#.
Javascript's object literal notation (pre-ES6) has no provision for variable keys. You could reduce the number of assignments a bit, though:
handleCellChange: function(rowIdx, prop, val) {
var updateObj = { data: {} };
(updateObj.data[rowIdx] = {})[prop] = { $set: val };
You may or may not consider that any improvement - more concise, but probably less readable. I would wholeheartedly recommend that you add the nested data object to the initial assignment to updateObj (which should be declared with var, btw) as I did above. However, using the result of the initialization of data[rowIdx] as a value that you then index on the same line is of rather more questionable value.

Categories

Resources