I have the following object like this:
{ where: { [Symbol(or)]: [ [Object], [Object] ] },hooks: true, rejectOnEmpty: false }
I'm calling JSON.stringify on this, and it gets converted to:
{"where":{},"hooks":true,"rejectOnEmpty":false}
I think this is because [Symbol(or)] evaluates to undefined so stringify removes it.
This value is coming from Sequelize operators, specifically Op.or. Is there a way stringify can convert this to a String so I would instead receive:
{"where":{"[Symbol(or)]": [[<<stringifiedObject>>], [<<stringifiedObject>>]]},"hooks":true,"rejectOnEmpty":false}
I know I could pass a function to JSON.stringify which would replace undefined with something, but I would like to maintain the original Symbol in the string replacement, so that I can distinguish between Symbol(and) and Symbol(or), even though both would evaluate to undefined.
Solved with the following function:
const cleanObject = (obj) => {
try {
const keys = Reflect.ownKeys(obj);
const ret = {};
keys.forEach((key) => {
let val = obj[key];
if (Object.prototype.toString.call(val) === '[object Object]') {
val = cleanObject(val);
}
if (typeof key === 'symbol') {
const newKey = `${String(key)}`;
ret[newKey] = val;
} else {
ret[key] = val;
}
});
return ret;
} catch (ex) {
console.log(ex);
}
};
Reflect.ownKeys returns all keys, including Symbols.
Next, I check if a given key is a Symbol and replace it with a string. I also call the function recursively to apply the same logic to all nested objects.
I have an object, sometimes it is empty like so {} other times it will have properties that are set to null.
{
property1: null,
property2: null
}
How can I determine if ALL the properties within this object is null?
If they are all null then return false.
At the moment I'm using lodash to check for the first case where the object is simply {} empty. But I also need to cover the second case.
if (isEmpty(this.report.device)) {
return false;
}
return true;
You can use Object.values to convert the object into array and use every to check each element. Use ! to negate the value.
let report = {
property1: null,
property2: null,
}
let result = !Object.values(report).every(o => o === null);
console.log(result);
An example some elements are not null
let report = {
property1: null,
property2: 1,
}
let result = !Object.values(report).every(o => o === null);
console.log(result);
Doc: Object.values(), every()
Approach using .some() instead of .every():
function isEmpty (obj) {
return !Object.values(obj).some(element => element !== null);
}
This function (named isEmpty to match the name given in the question) shall return false if any obj property is not null and true otherwise.
You can use the Object.keys() method this will return all keys in that Object as an Array. This makes it possible to do Object.keys(this.report.device).filter(key => !this.report.device[key] === null), which will return you the amount of not null keys, if this is 0 then you have your answer.
In essence relying on null properties is not such a good approach it's better to make those properties undefined or just to return a flat Object from your API.
Hope this helped.
This is very simple and can be done with a one liner !
function IsAllPropertiesNull(obj) {
return Object.values(obj).every(v=>v == null);
}
a = {'a': null, 'b':null};
var isAllNull = IsAllPropertiesNull(a)
// isAllNull = true
explanation -
get all values of object - iterate them and check for null
Good luck!
Use Object.entries and Array.every
let obj = {
property1: null,
property2: null,
};
function isEmpty(o) {
return Object.entries(o).every(([k,v]) => v === null);
}
if(isEmpty(obj)) {
console.log("Object is empty");
}
In order to check if object has null value for all the keys. One cleaner approach could be.
const set = new Set(Object.values(obj));
const hasOnlyNullValues = set.size === 1 && set.has(null);
For Es2015
if (Object.keys(obj).map(e => obj[e]).every(a => a.length === 0)) {
console.log('all are empty');
}
I have an array of objects, I would like to remove 'undefined' from any of the properties in any of the objects.
To remove undefined from an object, I use this method,
removeNullorUndefined:function(model) {
function recursiveFix(o) {
// loop through each property in the provided value
for (var k in o) {
// make sure the value owns the key
if (o.hasOwnProperty(k)) {
if (o[k] === 'undefined') {
// if the value is undefined, set it to 'null'
o[k] = '';
} else if (typeof (o[k]) !== 'string' && o[k].length > 0) {
// if there are sub-keys, make a recursive call
recursiveFix(o[k]);
}
}
}
}
var cloned = $.extend(true, {}, model);
recursiveFix(cloned);
return cloned;
},
How can I modify this so it can also accept an array of objects and remove 'undefined' from it ?
Appreciate any inputs
As long as the value is undefined and not a string value of 'undefined' then one way is to use JSON.stringify. Referring to property values:
If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array). JSON.stringify can also just return undefined when passing in "pure" values like JSON.stringify(function(){}) or JSON.stringify(undefined).
So, you could stringify an object and immediately parse it to remove undefined values.
NOTE: This approach will deep clone the entire object. In other words if references need to be maintained this approach won't work.
var obj = {
foo: undefined,
bar: ''
};
var cleanObj = JSON.parse(JSON.stringify(obj));
// For dispaly purposes only
document.write(JSON.stringify(cleanObj, null, 2));
An added bonus is without any special logic it will work at any depth:
var obj = {
foo: {
far: true,
boo: undefined
},
bar: ''
};
var cleanObj = JSON.parse(JSON.stringify(obj));
// For dispaly purposes only
document.write(JSON.stringify(cleanObj, null, 2));
If it is a string value of 'undefined' you can use the same approach but with a replacer function:
var obj = {
foo: {
far: true,
boo: 'undefined'
},
bar: ''
};
var cleanObj = JSON.parse(JSON.stringify(obj, replacer));
function replacer(key, value) {
if (typeof value === 'string' && value === 'undefined') {
return undefined;
}
return value;
}
// For dispaly purposes only
document.write(JSON.stringify(cleanObj, null, 2));
If you like the way removeNullorUndefined() currently works then you might try:
items.forEach(function(item){ removeNullorUndefined(item); });
i want to find a value in JSON stringify array
[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]
my idea to get this value is to know if what i am adding is not already added
// stringified JSON
stringifiedJson = '[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]';
// parse the stringified JSON into a JavaScript object
parsedJson = JSON.parse(stringifiedJson);
// the object in the array you want to check
number = 0
// check if the property exists
if(typeof parsedJson[number].humidity !== 'undefined') {
// set the property
parsedJson.humidity = 1;
}
// stringify your object again
stringifiedJson = JSON.stringify(parsedJson);
EDIT: here is a propertyExists function:
var propertyExists = function(stringifiedJson, id, property) {
// parse the stringified JSON into a JavaScript object
parsedJson = JSON.parse(stringifiedJson);
// check if the property exists for a given ID
for(var i = 0; i < parsedJson.length; i += 1) {
if(parseInt(parsedJson[i].id) === parseInt(id)) {
return (typeof parsedJson[i][property] !== 'undefined')
}
}
return false;
}
// stringified json
var stringifiedJson = '[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]';
console.log(propertyExists(stringifiedJson, 432, 'humidity'));
....
var jsonArray = [{"id":"432","temperature":"1","humidity":"1",.....
var isPresent = false;
$.each(jsonArray, function(i,v){
if(jsonArray[i].id == newObject.id){
isPresent = true;
}
});
if(!isPresent){
jsonArray.push(newObject);
}
....
i want to find a value in JSON stringify array
You don't say what value you are trying to find. What you've posted is an Array literal, not JSON. If you want to iterate over the array to determine if it contains an object with a certain property that has a particular value, you can use Array.prototype.some:
function hasPropValue(array, prop, value) {
return array.some(function(obj) {
return obj.hasOwnProperty(prop) && obj[prop] === value;
}
}
some will iterate only over members that exist, and will stop as soon as the callback returns true, otherwise it returns false. If you really do have a string value that is valid JSON, you can do:
hasPropValue(JSON.parse(jsonText), 'id', '432');
var jsonText = '[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]'
function hasPropValue(array, prop, value) {
return array.some(function(obj) {
return obj.hasOwnProperty(prop) && obj[prop] === value;
});
}
document.write(hasPropValue(JSON.parse(jsonText), 'id', '432'));
I have this code:
var showRegion = function(key) {
if (key in regionOptions) {
var entry = regionOptions[key];
var builder = entry.builder;
var layoutObj = entry.layoutObj;
var viewStarter = entry.viewStarter;
var view = new builder();
logger.info('Controller.' + key + ' => CreateAccountLayoutController');
Controller.layout[layoutObj].show(view);
view[viewStarter]();
}
};
What I need is that the parameter should be able to accept an array or a string, and should work either way.
Sample function calls:
showRegion('phoneNumberRegion');
showRegion(['phoneNumberRegion', 'keyboardRegion', 'nextRegion']);
This post is old, but here is a pretty good tip:
function showRegions(keys) {
keys = [].concat(keys)
return keys
}
// short way
const showRegions = key => [].concat(keys)
showRegions(1) // [1]
showRegions([1, 2, 3]) // [1, 2, 3]
var showRegion = function(key) {
if (typeof key === 'string')
key = [key];
if (key in regionOptions) {
...
No need to make a code for each case, just convert key string into an array of one element and the code for arrays will do for both.
you could use typeof to check for the type of your argument and proceed accordingly, like
var showRegion = function(key) {
if( typeof key === 'object') {
//its an object
}
else {
//not object
}
}
You can use the fact that string.toString() always returns the same string and Array.toString() returns a comma-delimited string in combination with string.split(',') to accept three possible inputs: a string, an array, a comma-delimited string -- and reliably convert to an array (provided that you're not expecting commas to be part of the values themselves, and you don't mind numbers becoming strings).
In the simplest sense:
x.toString().split(',');
So that
'a' -> ['a']
['a','b'] -> ['a','b']
'a,b,c' -> ['a','b','c']
1 -> ['1']
Ideally, you may want to tolerate null, undefined, empty-string, empty-array (and still keep a convenient one-liner):
( (x || x === 0 ) && ( x.length || x === parseFloat(x) ) ? x.toString().split(',') : []);
So that also
null|undefined -> []
0 -> ['0']
[] -> []
'' -> []
You may want to interpret null/empty/undefined differently, but for consistency, this method converts those to an empty array, so that downstream code does not have to check beyond array-having-elements (or if iterating, no check necessary.)
This may not be terribly performant, if that's a constraint for you.
In your usage:
var showRegion = function(key) {
key = ( (key || key === 0 ) && ( key.length || key === parseFloat(key) ) ? key.toString().split(',') : []);
/* do work assuming key is an array of strings, or an empty array */
}