Determine whether a specific property of an object is equal to null or undefined

I am trying to work with an object structured like this :

    $scope.contact = {
        name: contact.gd$name.gd$givenName.$t,
        familyName: contact.gd$name.gd$familyName.$t,
        phone: contact.gd$phoneNumber[0].$t,
        mail: contact.gd$email[0].address,
        organization: contact.gd$organization[0].gd$orgTitle.$t
    };

Is there a way to efficiently inspect this object to check if any of its properties are null or undefined, and then report which ones they are?

I could use a loop for this task, but I am curious if there is a quicker method available.

Answer №1

Here's a way to do it:

let emptyProps = Object.keys($scope.contact).filter(function(key) {
    return $scope.contact[key] === null || typeof $scope.contact[key] === "undefined";
});

This code snippet loops through all the properties of the object $scope.contact and only selects those with values that are either null or undefined.

Answer №2

If you want to create a function that filters out keys with null or undefined values, you can use the following helper function:

const exampleObj = {
    name: 'John',
    age: 30,
    city: null,
    country: undefined,
    active: false
}

const filterEmptyKeys = obj => Object.keys(obj).filter(key => obj[key] == null)

const result = filterEmptyKeys(exampleObj);

console.log(result);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing CodeIgniter Controller for Handling JSON Requests via AJAX

My challenge lies in sending a form using CodeIgniter via AJAX and attempting to receive the response in JSON format. However, I encounter an issue where I can only view the response when I open my developer tab (although unsure if that's the actual r ...

Setting the title attribute for option elements in a select element that is bound to a model using AngularJs

I'm currently working on an HTML select element that is connected to an ng-model. Here's what the setup looks like: <select data-ng-model="accountType" data-ng-options="at.name for at in accountTypes"></select> This is how my accou ...

I'm having trouble getting my home.js to render in the outlet component

After using Material-UI to create navbar.js, I encountered an issue where the other component was not rendering in the <Outlet/> Component RootLayout.js import { Outlet } from "react-router-dom"; import NavBar from "../component/NavBa ...

Optimal configuration for deploying an AngularJS Node.js application

I am developing a Moto Adverts application using angularjs and nodejs. Currently, the client-side in Angularjs is running on Apache HTTP Server (localhost:8000) while the server-side in Node.js is running on its own http server (localhost:3000). Snippet o ...

Encountered an issue with reading null properties when trying to access 'defaultPrevented' in bootstrap 5 toast

Implementing a custom hook to display and hide toast messages is working perfectly fine when invoking showToast in the button component of the Toast, but encountering an error when using this hook outside, for example in the button component of the App. Ty ...

Callback in React Setstate triggered, leading to a delay in rendering

Recently, I embarked on a journey to learn React just 2 days ago. Despite my enthusiasm, I have encountered some challenges with React's setState method. As far as my understanding goes, I should utilize the prevState parameter when I need to alter th ...

Receiving time slots on a bootstrap schedule

I recently developed a calendar using Bootstrap that allows users to select a start date and automatically sets the end date within a 7-day range from the chosen start date. However, I am facing a challenge in enabling users to also pick minutes along with ...

html input type called array named AmountMap[1] is unable to be configured with <input type="textbox" size="15" name="amountMap[1]" value="0" >

**Having trouble setting the value of an HTML input type named like an array AmountMap[1] <input type="textbox" size="15" name="amountMap[1]" value="0" > When trying to set amountMap[1].value='10', ...

AngularJS Karma unit tests experiencing issues with loading Jasmine matcher functions

Currently, I am in the process of developing unit tests for an AngularJS application. For testing purposes, I have opted to use the Karma test runner along with the Jasmine framework. The specific function that is being tested aims to retrieve objects fro ...

I want my subheading to feature a trio of buttons

When trying to add 3 buttons to my sub-header, the layout appears cluttered like this: <div class="bar bar-subheader"> <div class=" button-bar"> <button class="button button-positive" style="padding-right:25%;"> C ...

Using AJAX to dynamically update a div's class following a POST request

Upon double clicking a row, I am attempting to trigger the function outlined below. Despite my efforts, I have not been able to update the div class with any changes. function book(id) { $.ajax({ type: "POST", url: "ajax_servlet. ...

Reserved space for both double and single quotation marks

I've created a function that generates a table of specified size with predetermined content in each cell, and displays it within a designated div ID. generate_table(4, 4, 'Cell Content', 'display') To test this function, I added a ...

What is the best way to create a JavaScript Up/Down Numeric input box using jQuery?

So, I have this Numeric input box with Up/Down buttons: HTML Markup: <div class="rotatortextbox"> <asp:TextBox ID="txtrunningtimeforfirstlot" ClientIDMode="Static" runat="server">0</asp:TextBox> (In mins) </div> <div cl ...

Error Encountered: Unable to Convert String to JSONObject in Android Application

When fetching data from a MySQL database using JSON and setting the data in a ListView Item, encountering an issue where the Java code is working fine but unable to get the value in the listview. Need assistance on how to convert the JSON value. Looking f ...

Modifying AngularJS/UI-Router state using select and option elements

I am working on a SPA using AngularJS/UI-Router and facing an issue with changing states from a select/option in one of the sections. Initially, I had links in an ng-repeat setup like this: <ul class="unstyled"> <li data-ng-repeat="course in ...

I'm experiencing trouble with Shopify as it appears to be failing to execute

I have been attempting to incorporate a tracking pixel into my project. Thus far, I have tested the code in various environments, both with and without wrapping it in a function and deploying it using window.onload. If you would like to see the implementa ...

What is prohibiting me from producing a random number?

Is there an issue with generating a random number in this input box by simply clicking the button? function myFunction() { var x = document.getElementById("demo"); x.innerHTML = Math.floor((Math.random() * 100) + 1); } <button onclick="myFu ...

Refreshing the Angular directive following a change in the scope variable

Just getting started with Angular and I've encountered a small issue. Within my template for SirroccoListing, I have the following code. Sirrocco is a directive: <h3>All Sirroccos</h3> <div sirrocco ng-repeat="sirrocco in sirroccos" ...

Receiving input from the "Enter" key is functional in Internet Explorer but not Firefox when utilized in Cognos

Working with Cognos, I'm currently tackling the challenge of capturing the enter key to validate user input before submitting the prompt. If you're not familiar with Cognos, that's okay - any insights on my code are welcome :) This tool is ...

How can I retrieve the "src" value of an iframe using PHP?

I have a webpage containing an iframe, where upon clicking a menu item, the iframe's URL is updated based on the selection. This functionality is achieved by invoking a JavaScript function on 'onclick', which passes the selected URL from th ...