logo
down
shadow

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


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

By : user2954153
Date : November 22 2020, 01:01 AM
fixed the issue. Will look into that further 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. , Create a function to loop and check:
code :
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


Share : facebook icon twitter icon
How to convert an empty array and empty object to an empty string or null

How to convert an empty array and empty object to an empty string or null


By : Akis
Date : March 29 2020, 07:55 AM
it helps some times How can I convert an empty array to an empty string or null? , What about this:
code :
function convert($array) {
    return (count($array) === 0) ? "" : $array;
}

$empty_array = array();
$empty_array = convert($empty_array);
function convert($object) {
    return (count(get_object_vars($object)) === 0) ? "" : $object;
}
Best practice rendering model attributes: where check whether they are null or string empty

Best practice rendering model attributes: where check whether they are null or string empty


By : Wesley Hudson
Date : March 29 2020, 07:55 AM
this will help I don't think there's anything wrong with checking for null values in the view, I suppose the alternative would be to create a viewmodel for every eventuality and do the checking in the controller.
Check for null & replace it with empty string for all properties in a object using jQuery and JavaScript

Check for null & replace it with empty string for all properties in a object using jQuery and JavaScript


By : bazyliszek100
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You can use JSON.Stringify (see MDN) with a replacer method to replace all nulls in an Object:
code :
console.log(replaceNull({
    x: {},
    y: null,
    z: [1,null,3,4],
    foo: "foo",
    bar: {foobar: null}
  }));

const yourObj = { "person": { "id": 12345, "name": "John Doe", "phones": { "home": "800-123-4567", "mobile": null }, "email": [ "jd@example.com", "jd@example.org" ], "dateOfBirth": null, "registered": true, "emergencyContacts": [ { "name": "Jane Doe", "phone": null, "relationship": "spouse" }, { "name": "Justin Doe", "phone": "877-123-1212", "relationship": undefined } ] } };

console.log(replaceNull(yourObj, ""));

function replaceNull(someObj, replaceValue = "***") {
  const replacer = (key, value) => 
    String(value) === "null" || String(value) === "undefined" ? replaceValue : value;
  //^ because you seem to want to replace (strings) "null" or "undefined" too
  
  return JSON.parse( JSON.stringify(someObj, replacer));
}
Javascript how to set null/undefined values in an Object to an empty string

Javascript how to set null/undefined values in an Object to an empty string


By : user2753113
Date : March 29 2020, 07:55 AM
hope this fix your issue You can use _.mapValues() to iterate the values and replace them, and _.isNil() to check if a value is null or undefined:
code :
const obj = {
  a: '23',
  b: 'Red',
  c: null,
}

const result = _.mapValues(obj, v => _.isNil(v) ? '' : v)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
If object contains null value how to assign empty string in javascript?

If object contains null value how to assign empty string in javascript?


By : Jrebolledo
Date : October 01 2020, 05:00 PM
I hope this helps . I have an object,
code :
$.each(data,function(i,e){
  .........
  $(".email").text(e && e.email ? e.email : '');
  .........
})
Related Posts Related Posts :
  • RangeError: Maximum call stack size exceeded with array.slice
  • node rest client get with local variable
  • If condition not executes while mouse move fastly
  • Count the number of displayed elements in a HTML list
  • Locate JavaScript source code in Emacs
  • Clean, Modular Code vs MV* Frameworks
  • infinite scroll without loading image
  • Backbone: reverse collection order with comparator
  • What do printers ignore?
  • jQuery UI .tabs() Contentless tab?
  • Execute Javascript alert() after page (visually) loaded
  • JavaScript - duplicating array doesn't work
  • Excessive clickable area below image
  • JavaScript Regex: Replace |b| with <b>
  • Unexpected value change in 2D array in JavaScript
  • Function doesnt see parameter as a array of objects
  • jQuery fetch keypress event on chrome and IE
  • How to enable jquery validate localization?
  • Cassandra map collection returned by node.js Helenus looks odd
  • angular ng-repeat with multiple filter options
  • Selecting Children without ID or Class names
  • How to uncheck a group of checkboxes when another checkbox is checked
  • Is hiding content by Javascript or jQuery Worth trying
  • Load XUL resource using javascript
  • XML to HTML text area from server file system
  • set focus() on textbox on form onload
  • es lint '' is assigned a value but never used' eventhough i have used it
  • Can "name" attribute be used for custom VueJS components
  • Get innerHTML of content when it is clicked
  • HTTPS causes CSS animations to not load? Very confused
  • How to hide other div apart from touched div to the band
  • Is global variable assignment atomic on NodeJS?
  • Datepicker onchangemonthyear beforeshowday
  • trying to render html files using jade but it still adresses it as a jade
  • Google Analytics - Understanding and Dissecting the Snippet: What is | | [ ];
  • Replacing "[aA09.b]." to "[aA09.b]\n" in in JavaScript
  • Why wrap a function definition in an immediate function?
  • Enable / disable jQuery script on switching to different tab in a HTML page
  • how can find return variable value outside anonymous function in node js mysql query function
  • Converting 1 to 0001 in javascript array
  • svg viewbox should not resize the text fontSize?
  • store newline character with string in a variable
  • MP4 videos in Flowplayer does not play in Windows 7 Chrome
  • jquery focusin event not firing
  • How to debug Vue application with google chrome
  • html 5 web app cache download complete javascript alert?
  • Ember.js setupController fired only once
  • How to validate a currency input
  • How to convert my Array of data to key & value pair
  • Statistics circles in CSS
  • So I wanted to make a trig solver in javascript?
  • Showing HighCharts series name on x-axis and in legend
  • In Rails, how can we pass parameters in script?
  • Build Fails: `npm rebuild node-sass --force`
  • Angular Datepicker change dateformat
  • Trying to extract data from between two sets of characters
  • Configuring Jest in WebStorm
  • Uncaught Error: Syntax error, unrecognized expression: tr.ec_portfolio_title,
  • JQuery fading in fading out continuously
  • Unexpected end of input (Line 1) and Undefined Function?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org