How check object to undefined? - javascript

I have had object
dataSourceConfig.transport.read.url
remoteDataSourceConfig: {
transport: {
read: {
url: null,
},
},
},
How to check dataSourceConfig.transport.read.url for undefiend?

If I understand your question crrectly, you have to check all the intermediary objects for existence:
if (dataSourceConfig &&
dataSourceConfig.transport &&
dataSourceConfig.transport.read &&
dataSourceConfig.transport.read.url != null
) {
// is not null or undefined
}
Otherwise you can do something like this:
function isKeySet(obj, key) {
var keys = key.split('.');
return keys.reduce(function(o,k){ return o[k]; }, obj) != null;
}
isKeySet(dataSourceConfig, 'transport.read.url'); // false

Related

Filtering Everything But Functions

I have this object
{
helloWorld: function () {
console.log("Test")
},
debug: false,
foo: {
test: "test",
bar: function () {
console.log(false)
}
}
}
However, programmatically I want it to look like this:
{
helloWorld: function() {
console.log("Test")
},
foo: {
bar: function() {
console.log(false)
}
}
}
Basically removing everything but the functions of an object.
You could do a recursive call. For every key-value pairs of the object, check the value:
if it is function, keep it
else do a recursive call on that value
Base condition for on recursive call
if it is not object, return null
if the object is empty, also return null
After map through the key-value pairs, filter the pairs with value not equal null.
Finally, transform the pairs back to object
function keepFunc(obj) {
if (!isObject(obj)) {
return null
}
if (Object.keys(obj).length === 0) {
return null
}
return Object.fromEntries(
Object.entries(obj)
.map(([key, value]) => [
key,
isFunction(value) ? value : keepFunc(value)
])
.filter(([key, value]) => value !== null)
)
}
Runnable example
const obj = {
helloWorld: function() {
console.log('Test')
},
debug: false,
moreDebug: {},
foo: {
test: 'test',
bar: function() {
console.log(false)
},
moreTest: {
weather: 'cool',
say: function () {
console.log('phew')
}
}
}
}
const isObject = obj => typeof obj === 'object' && obj !== null
const isFunction = func => typeof func === 'function'
function keepFunc(obj) {
if (!isObject(obj)) {
return null
}
if (Object.keys(obj).length === 0) {
return null
}
return Object.fromEntries(
Object.entries(obj)
.map(([key, value]) => [
key,
isFunction(value) ? value : keepFunc(value)
])
.filter(([key, value]) => value !== null)
)
}
console.log(keepFunc(obj))
References
Object.entries(): to transform object into key-value pairs
Object.fromEntries(): to transform key-value pairs into object
You can use recursive function call in javascript to achieve that. For each key in the object check if it is an object or function and if it is keep it:
var input = {
helloWorld: function() {
console.log("Test")
},
debug: false,
foo: {
test: "test",
bar: function() {
console.log(false)
}
}
};
function buildObjectsOnlyObject(obj) {
let retVal = {};
for (let key in obj) {
const val = obj[key];
if (typeof val === 'object') {
if (!val) { // undefined and null also have object type
continue;
}
if (val.__proto__ === Array.prototype) { // check if object is an array
retVal[key] = val;
} else {
retVal[key] = buildObjectsOnlyObject(obj[key]);
}
} else if (typeof val === 'function') {
retVal[key] = val;
}
}
return retVal;
}
console.log(buildObjectsOnlyObject(input));
Object.entries and Object.fromEntries help here, as does Array.prototype.some.
For objects, filter the object's entries and keep only entries whose values are functions or objects with descendants that are functions. This can be checked recursively.
Then let each value be either the function or the stripped nested object.
This strip function will return undefined if nothing is kept.
let example =
{
helloWorld: function () {
console.log("Test")
},
debug: false,
foo: {
test: "test",
bar: function () {
console.log(false)
}
}
};
let isobj = val => typeof val == 'object' && val !== null;
let isfn = val => typeof val == 'function';
let keep = val => isobj(val) ? Object.entries(val).some(keepEntry) : isfn(val)
let keepEntry = ([key, val]) => keep(val);
let stripEntry = ([key, val]) => [key, strip(val)];
let strip = val => keep(val) ? isobj(val) ?
Object.fromEntries(Object.entries(val).filter(keep).map(stripEntry)) :
isfn(val) ? val : undefined : undefined
console.log(strip(example));
(Surprisingly this took more code than I thought it would.)

Removing an empty array from an object with JavaScript

I have this JSON object myFilters:
{"filters":
{"role":"","jobs":[]}
}
I can correctly remove the empty object from it with this function clean (myFilters):
function clean(obj) {
for (var propName in obj) {
if (
obj[propName] === null ||
obj[propName] === undefined ||
obj[propName] === ""
) {
delete obj[propName];
}
}
So now, my myFilters object becomes:
{"filters":
{ "jobs":[] }
}
How can I now remove the empty array and the key from my JSON object?
You should add one more condition like
function clean(obj) {
for (var propName in obj) {
if (
obj[propName] === null ||
obj[propName] === undefined ||
obj[propName] === "" ||
Array.isArray(obj[propName]) && obj[propName].length === 0
) {
delete obj[propName];
}
}
}
You should check the type of property before check its value by the
typeof
The jobs property is an object and you can check its value by its
length. it is empty if its length equals to 0.
function clean(obj) {
for (var propName in obj) {
if (typeof (obj[propName]) == 'object') {
if (obj[propName].length == 0) {
delete obj[propName];
}
} else {
if (
obj[propName] === null ||
obj[propName] === undefined ||
obj[propName] === ""
) {
delete obj[propName];
}
}
}
}
I like Saveli Tomac's solution, so I upvoted that. Let me show you an additional shortening on the original solution also.
As it's been stated that you need to check 2 more things if you are looking for an empty array. So what about checking null, undefined and '' values easier?
if (!undefined) { console.log('undefined needs to be deleted') };
if (!null) { console.log('null needs to be deleted') };
if (!'') { console.log(`'' needs to be deleted`) };
Checking Array.length if it has 0 value can be also shorter just like the following:
const array1 = [];
const array2 = [1,2,3];
if (!array1.length) { console.log('array1 has 0 length') };
if (!array2.length) { console.log('array2 has 0 length') };
So based on those code snippets you can have an additional shortening just like the following:
// extended with other types for the demo
let myObject = { "filters": { "role": "", "jobs": [], "nullValue": null, "undefinedIsHere": undefined, "arrayWithValue": [1,2,3], "stringValue": "hello", "numberishere": 123 } };
const clean = (obj) => {
for (let propName in obj) {
if (
!obj[propName] ||
Array.isArray(obj[propName]) && !obj[propName].length
) { delete obj[propName] };
}
}
clean(myObject.filters);
console.log(myObject);
Or with a 1️⃣ liner:
// extended with other types for the demo
let myObject = { "filters": { "role": "", "jobs": [], "nullValue": null, "undefinedIsHere": undefined, "arrayWithValue": [1,2,3], "stringValue": "hello", "numberishere": 123 } };
const clean = (obj) => {
Object.keys(obj).forEach(propName => (!obj[propName] || Array.isArray(obj[propName]) && !obj[propName].length) && delete obj[propName]);
}
clean(myObject.filters);
console.log(myObject);
Read further here:
Array.isArray()
Array.length
I hope this helps!
Try this :
var filterObj = {
"filters": {
"role": "",
"jobs": []
}
};
for (var i in filterObj) {
for (var j in filterObj[i]) {
if ((filterObj[i][j] === null) ||
(filterObj[i][j] === undefined) ||
(filterObj[i][j].length === 0)) {
delete filterObj[i][j];
}
}
}
console.log(filterObj);
Saveli's answer should work fine. Here's an alternative approach you can use to achieve the same result.
const object = {
"filters": {
"role": "",
"jobs": [],
"foo": undefined,
"baz": null,
"bar": {},
"moreJobs": ['1', '2']
}
}
const result = {
filters: Object.keys(object.filters).reduce((acc, key) => {
if (
object.filters[key] !== null &&
object.filters[key] !== undefined &&
object.filters[key] !== '' &&
typeof object.filters[key] === 'object' && Object.keys(object.filters[key]).length > 0
) {
acc[key] = object.filters[key];
}
return acc;
}, {})
};
console.log(result);
It should be like this:
function clean(obj) {
for (var propName in obj) {
if (obj.hasOwnProperty(propName) &&
obj[propName] === null ||
obj[propName] === undefined ||
obj[propName] === "" ||
(Array.isArray(obj[propName]) && obj[propName].length <= 0)
) {
delete obj[propName];
}
}
}

How the java script complex object and complex array iterate?

Below is running code snippet for the javascript object and array.
I have one jsonObj and here the ResultElementLevel could be the array or
object.
According to I just put if else condition and compare if Array and 'object'.
My question is,How would it be possible without if else condition?
can we write one function which compare object and Array inside single if.
The jsonObj is populating dynamically.
Here it would be possible CHECK object is also come into the Array or Object.
var jsonObj = {
"Response": {
"Errors": {
"Check": {
"_attributes": {
"id": "51416",
"name": "lucyocftest090601"
},
"CheckLevel": {
},
"ResultElementLevel": {
"_text": "Line No (2) [Missing Reporting Category] "
}
}
},
"Success": {
}
}
}
iterateObjorArr(jsonObj);
function iterateObjorArr(jsonObj){
let checkArr = jsonObj.Response.Errors.Check;
let checkID = checkArr._attributes.id;
let checkName = checkArr._attributes.name;
let status = 'failed';
let resultElementLevel = checkArr.ResultElementLevel;
let errorUploadArr = [];
let errorUploadObj;
if (Array.isArray(resultElementLevel)) {
resultElementLevel.map(function (data, index) {
errorUploadObj = {
'id': checkID,
'checkName': checkName,
'status': status,
'errors/warnings': data._text
};
errorUploadArr.push(errorUploadObj);
});
} else {
if (typeof (resultElementLevel) === 'object') {
errorUploadObj = {
'id': checkID,
'checkName': checkName,
'status': status,
'errors/warnings': resultElementLevel._text
};
errorUploadArr.push(errorUploadObj);
}
}
console.log("errorUploadArr", errorUploadArr);
}
You can test to see if resultElementLevel has the length property or not using hasOwnProperty(). Arrays have a length while objects do not (generally):
if (resultElementLevel.hasOwnProperty('length')) {
// Handle it as an array
} else {
// Handle as an object
}
This will, however, only work if the object assigned to resultElementLevel is guaranteed to not have a length property.
My question is,How would it be possible without if else condition? can we write one function which compare object and Array inside single if.
I don't think you'd want to get rid of the condition, but being able to deal with the passed data the same way, wether it's an array, a single item, or null/undefined
You could normalize the data first
function toArray(value){
return value == null? []:
Array.isArray(value)? value:
//isArrayLike(value)? Array.from(value):
[value];
}
//Objects that look like Arrays
function isArrayLike(value){
return value !== null && typeof value === "object" && value.length === (value.length >>> 0);
}
so that from here on, you always deal with an Array:
let errorUploadArr = toArray(checkArr.ResultElementLevel)
.map(function(item){
return {
id: checkID,
checkName: checkName,
status: status,
"errors/warnings": item._text
};
});
var jsonObj = {
Response: {
Errors: {
Check: {
_attributes: {
id: "51416",
name: "lucyocftest090601"
},
CheckLevel: {},
ResultElementLevel: {
_text: "Line No (2) [Missing Reporting Category] "
}
}
},
Success: {}
}
};
iterateObjorArr(jsonObj);
function toArray(value) {
return value == null ? [] :
Array.isArray(value) ? value :
//isArrayLike(value)? Array.from(value):
[value];
}
//Objects that look like Arrays
function isArrayLike(value) {
return value !== null && typeof value === "object" && value.length === (value.length >>> 0);
}
function iterateObjorArr(jsonObj) {
let checkArr = jsonObj.Response.Errors.Check;
let checkID = checkArr._attributes.id;
let checkName = checkArr._attributes.name;
let status = "failed";
let errorUploadArr = toArray(checkArr.ResultElementLevel)
.map(function(data) {
return {
id: checkID,
checkName: checkName,
status: status,
"errors/warnings": data._text
}
});
console.log("errorUploadArr", errorUploadArr);
}
.as-console-wrapper{top:0;max-height:100%!important}

Determining if all attributes on a javascript object are null or an empty string

What is the most elegant way to determine if all attributes in a javascript object are either null or the empty string? It should work for an arbitrary number of attributes.
{'a':null, 'b':''} //should return true for this object
{'a':1, 'b':''} //should return false for this object
{'a':0, 'b':1} //should return false
{'a':'', 'b':''} //should return true
Check all values with Object.values. It returns an array with the values, which you can check with Array.prototype.every or Array.prototype.some:
const isEmpty = Object.values(object).every(x => x === null || x === '');
const isEmpty = !Object.values(object).some(x => x !== null && x !== '');
Create a function to loop and check:
function checkProperties(obj) {
for (var key in obj) {
if (obj[key] !== null && obj[key] != "")
return false;
}
return true;
}
var obj = {
x: null,
y: "",
z: 1
}
checkProperties(obj) //returns false
Here's my version, specifically checking for null and empty strings (would be easier to just check for falsy)
function isEmptyObject(o) {
return Object.keys(o).every(function(x) {
return o[x]===''||o[x]===null; // or just "return o[x];" for falsy values
});
}
let obj = { x: null, y: "hello", z: 1 };
let obj1 = { x: null, y: "", z: 0 };
!Object.values(obj).some(v => v);
// false
!Object.values(obj1).some(v => v);
// true
Using Array.some() and check if the values are not null and not empty is more efficient than using Array.every and check it the other way around.
const isEmpty = !Object.values(object).some(x => (x !== null && x !== ''));
This answer should just make the excellent comment of user abd995 more visible.
Quick and simple solution:
Object.values(object).every(value => !!value);
You can use the Array.reduce prototype on your object's keys.
Assuming that the object is structured as follows:
var obj = {
x: null,
y: "",
z: 1
}
you can use the following instruction to discover if all of it's properties are unset or set to empty string using just one line:
Object.keys(obj).reduce((res, k) => res && !(!!obj[k] || obj[k] === false || !isNaN(parseInt(obj[k]))), true) // returns false
If you want to discover if all of it's properties are set instead you have to remove the negation before the conditions and set the initial result value to true only if the object has keys:
Object.keys(obj).reduce((res, k) => res && (!!obj[k] || obj[k] === false || !isNaN(parseInt(obj[k]))), Object.keys(obj).length > 0) // returns false as well
Based on adeneo's answer, I created a single line condition. Hope it will be helpful to someone.
var test = {
"email": "test#test.com",
"phone": "1234567890",
"name": "Test",
"mobile": "9876543210",
"address": {
"street": "",
"city": "",
"state": "",
"country": "",
"postalcode": "r"
},
"website": "www.test.com"
};
if (Object.keys(test.address).every(function(x) { return test.address[x]===''||test.address[x]===null;}) === false) {
console.log('has something');
} else {
console.log('nothing');
}
You can test it https://jsfiddle.net/4uyue8tk/2/
Just complementing the past answers: they'll work if your object doesn't contain arrays or objects. If it does, you'll need to do a 'deep check'.
So I came up with this solution. It'll evaluate the object as empty if all its values (and values inside values) are undefined, {} or [].
function deepCheckEmptyObject(obj) {
return Object.values(obj).every( value => {
if (value === undefined) return true;
else if ((value instanceof Array || value instanceof Object) && _.isEmpty(value) ) return true;
else if (value instanceof Array && !_.isEmpty(value)) return deepCheckEmptyArray(value);
else if (value instanceof Object && !_.isEmpty(value)) return deepCheckEmptyObject(value);
else return false;
});
}
function deepCheckEmptyArray(array) {
return array.every( value => {
if (value === undefined) return true;
else if ((value instanceof Array || value instanceof Object) && _.isEmpty(value)) return true;
else if (value instanceof Array && !_.isEmpty(value)) return deepCheckEmptyArray(value);
else if (value instanceof Object && !_.isEmpty(value)) return deepCheckEmptyObject(value);
else return false;
});
}
Note it uses Lodash's .isEmpty() to do the heavy work after we 'isolated' a value. Here, Lodash is imported as '_'.
Hope it helps!
Also if you are searching for only values are empty within the object,
Object.values({ key: 0, key2: null, key3: undefined, key4: '' }).some(e => Boolean(e))
// false
Object.values({ key: 0, key2: null, key3: undefined, key4: "hello" }).some(e => Boolean(e))
// true
Object.values({ key: 1, key2: "hello" }).some(e => Boolean(e))
// true
Based on tymeJv's answer =)
function checkProperties(obj) {
var state = true;
for (var key in obj) {
if ( !( obj[key] === null || obj[key] === "" ) ) {
state = false;
break;
}
}
return state;
}
var obj = {
x: null,
y: "",
z: 1
}
checkProperties(obj) //returns false
Hope it helps =)
This will give you all the keys from the object which is empty, undefined and null
Object.keys(obj).filter((k)=> {
if (obj[k] === "" || obj[k]===undefined || obj[k]===null) {
return k;
}
});
Building on top of other answers I would use lodash to check isEmpty on the object, as well as its properties.
const isEmpty = (object) => return _.isEmpty(object) || !Object.values(object).some(x => !_.isEmpty(x))
This skip the function attribute
function checkIsNull(obj){
let isNull=true;
for(let key in obj){
if (obj[key] && typeof obj[key] !== 'function') {
isNull = false;
}
}
return isNull;
}
var objectWithFunctionEmpty={
"name":undefined,
"surname":null,
"fun": function (){ alert('ciao'); }
}
var objectWithFunctionFull={
"name":undefined,
"surname":"bla bla",
"fun": function (){ alert('ciao'); }
}
checkIsNull(objectWithFunctionEmpty); //true
checkIsNull(objectWithFunctionFull); //false
This works with me perfectly:
checkProperties(obj) {
let arr = [];
for (let key in obj) {
arr.push(obj[key] !== undefined && obj[key] !== null && obj[key] !== "");
}
return arr.includes(false);
}
This will return true or false if there is at-least one value is empty or something like that.
You can use Object.values() method to get all the object's values (as an array of object's values) and then check if this array of values contains null or "" values, with the help of _.includes method prvided by lodash library.
const checkObjectProperties = obj => {
const objValues = Object.keys(obj);
if (_.includes(objValues, "") || _.includes(objValues, null)) {
return false;
} else {
return true
}
const incorrectObjProps = { one: null, two: "", three: 78 }
const correctObjProps = { one: "some string" }
checkObjectProperties(incorrectObjProps) // return false
checkObjectProperties(correctObjProps) // return true
}
I'll add my two sense:
Object.values(object).every(value => Boolean(value));
Solution:
function checkValues(obj) {
var objValues = Object.values(obj);
if (objValues.length < 1) return false;
return objValues.every((value) => {
if (value === null) return true;
if (typeof(value) == 'string')
if(!(value || false))
return true;
return false;
});
}
// OR
Object.values( obj ).every(
value => value === null || (typeof(value) == 'string' && !(value || false))
);
Testing:
checkValues({ a: null, b: '' });
// OR
Object.values({ a: null, b: '' }).every(
value => value === null || (typeof(value) == 'string' && !(value || false))
);
// Output: true
checkValues({ a: '', b: '' });
// OR
Object.values({ a: '', b: '' }).every(
value => value === null || (typeof(value) == 'string' && !(value || false))
);
// Output: true
checkValues({ a: 0, b: '' });
// OR
Object.values({ a: 0, b: '' }).every(
value => value === null || (typeof(value) == 'string' && !(value || false))
)
// Output: false
checkValues({ a: 0, b: 1 });
// OR
Object.values({ a: 0, b: 1 }).every(
value => value === null || (typeof(value) == 'string' && !(value || false))
)
// Output: false
checkValues({ a: 1, b: '' });
// OR
Object.values({ a: 1, b: '' }).every(
value => value === null || (typeof(value) == 'string' && !(value || false))
)
// Output: false
How about this?
!Object.values(yourObject).join('')

formatting json data to be camelCased

I get a json response from the server that looks something like this:
{
"Response": {
"FirstName": "John",
"LastName": "Smith",
"NickNames": {
"NameOne": "Johnny",
"NameTwo": "JohnS",
"NameThree": "Smithy"
},
"Success": true,
"Errors": []
}
}
Is there a way I can run this response through a function so that the key of each key value pair would be camelCased?
So the output would look something like:
{
"response": {
"firstName": "John",
"lastName": "Smith",
"nickNames": {
"nameOne": "Johnny",
"nameTwo": "JohnS",
"nameThree": "Smithy"
},
"success": true,
"errors": []
}
}
If someone could point me in the right direction, that'd be great.
Thanks.
You would give JSON.parse a reviver function that assigns values to new properties that are lower-cased.
function toCamelCase(key, value) {
if (value && typeof value === 'object'){
for (var k in value) {
if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {
value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
delete value[k];
}
}
}
return value;
}
var parsed = JSON.parse(myjson, toCamelCase);
More information about how it works in this SO answer.
The approach that user '#I Hate Lazy' suggested - using a 'reviver' function is - the right one. However his function didn't work for me.
Perhaps it is because I'm parsing a JSON array. Also I use Resharper and it complained about a code smell :) ('not all code paths return a value'). So I ended up using a function from another SO issue which did work for me:
function camelCaseReviver(key, value) {
if (value && typeof value === 'object') {
for (var k in value) {
if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {
value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
delete value[k];
}
}
}
return value;
}
Here is a functional recursive (ES6) approach.
function convertKeysToCamelCase(o) {
if (o === null || o === undefined) {
return o;
} else if (Array.isArray(o)) {
return o.map(convertKeysToCamelCase);
}
return typeof o !== 'object' ? o : Object.keys(o).reduce((prev, current) => {
const newKey = `${current[0].toLowerCase()}${current.slice(1)}`;
if (typeof o[current] === 'object') {
prev[newKey] = convertKeysToCamelCase(o[current]);
} else {
prev[newKey] = o[current];
}
return prev;
}, {});
}
// successfully tested input
const o = {
SomeNum: 1,
SomeStr: 'a',
SomeNull: null,
SomeUndefined: undefined,
SomeBoolean: true,
SomeNaN: NaN,
NestedObject: {
SomeSentence: 'A is for apple',
AnotherNested: {
B: 'is for blahblah'
}
},
NumArray: [1, 2, 3, 4],
StringArray: ['a', 'b', 'c'],
BooleanArray: [true, false],
ArrayOfArrays: [[1,2,], ['a','b']],
ObjectArray: [{Foo:'bar'}, {Hello:'world', Nested:{In:'deep'}}],
MixedArray: [1,'a', true, null, undefined, NaN, [{Foo:'bar'}, 'wat']]
}
const output = convertKeysToCamelCase(o);
console.log(output.mixedArray[6][0].foo); // 'bar'
#adamjyee Your solution works except for nested array of integers. A small fix could be:
function convertKeysToCamelCase (o) {
if (o === null) {
return null
} else if (o === undefined) {
return undefined
} else if (typeof o === 'number') {
return o
} else if (Array.isArray(o)) {
return o.map(convertKeysToCamelCase)
}
return Object.keys(o).reduce((prev, current) => {
const newKey = `${current[0].toLowerCase()}${current.slice(1)}`
if (typeof o[current] === 'object') {
prev[newKey] = convertKeysToCamelCase(o[current])
} else {
prev[newKey] = o[current]
}
return prev
}, {})
[Right to comment but lacking comment priviledge :(]
You need to write a recursive function that traverses the tree and returns a new tree where the keys in the objects have been updated. The recursive function would call itself to deal with any sub-objects it encounters.

Categories

Resources