Verify if the property in every element of the array is not empty

How can you determine if all employees have a non-null value for the SSN property in the given object?

Employees:
{ id: 0, name: "John", SSN: "1234" }
{ id: 1, name: "Mark", SSN: "1876" }
{ id: 2, name: "Sue", SSN: "98826" }

Answer №1

When determining if 0 is a valid SSN, you can utilize the following condition: Number(Employees[i].SSN) > 0

TS Playground

const Employees = [
  { id: 0, name: "A", SSN: "1234" }, // true
  { id: 1, name: "B", SSN: "1876" }, // true
  { id: 2, name: "C", SSN: "" }, // false
  { id: 3, name: "D", SSN: null }, // false
  { id: 4, name: "E", SSN: undefined }, // false
  { id: 5, name: "F" }, // false
  { id: 6, name: "G", SSN: "98826"}, // true
];

function hasSSN<T extends {SSN?: string | null | undefined}>(value: T): value is T & {SSN: string} {
  return Number(value.SSN) > 0;
}

for (const employee of Employees) {
  console.log(employee.name, hasSSN(employee));
}

Demo:

const Employees = [
    { id: 0, name: "A", SSN: "1234" },
    { id: 1, name: "B", SSN: "1876" },
    { id: 2, name: "C", SSN: "" },
    { id: 3, name: "D", SSN: null },
    { id: 4, name: "E", SSN: undefined },
    { id: 5, name: "F" },
    { id: 6, name: "G", SSN: "98826" }, // true
];
function hasSSN(value) {
    return Number(value.SSN) > 0;
}
for (const employee of Employees) {
    console.log(employee.name, hasSSN(employee));
}

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

Leverage information retrieved from API in a separate React component

Is there a way to fetch data from an API in one component and then access it in another component without having to re-fetch the data? For example, if I fetch some data and create a table in Tables.js, how can I use that same data in TableDetails.js withou ...

The AppBar in a secondary color is looking sleek and modern with the Select component

I am currently utilizing version 33 of material-ui-next: import * as mui from 'material-ui'; Within a component, I have an AppBar featuring a ToolBar and a Select: render() { return ( <mui.AppBar color="secondary"> <mui.To ...

Maximizing the use of JavaScript's prototype.js library: Passing parameters to callback functions

My understanding of JavaScript is limited to using it for Dynamic HTML. However, I am now exploring Ajax and facing an issue with the code below (taken from and modified to suit my requirements). I need to pass the update_id parameter to the onSubmit fun ...

Creating a dropdown menu for an extensive list of 100 menu items: a step-by-step guide

In my React JS front-end project, I have implemented a drop-down menu using Material-UI. Currently, the menu items are hardcoded which works fine for a small number of items. However, this approach becomes cumbersome when dealing with a larger number of ...

Accessing the current clicked item in $scope using an Angular Directive within ng-repeat

I have set up a custom directive called calendar which includes a date picker created in JQuery. To associate the ng-model with the date picker, I am using the following code successfully: var clickedID = "#" + $scope.indexid + "_datePicker"; $(clickedID ...

Using RxJS v5 for Sending a POST Request with Parameters

Snippet of my RxJS code: .mergeMap(action => { const user = store.getState().user; return ajax.post(`${config.API_BASE_URL}/api/v1/rsvps`, { rsvp: { meetup_id: action.payload, user_id: user.id, } }) .map(action => calenda ...

Executing a cURL request using Node.js

Looking for assistance in converting the request below: curl -F <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a777f7e737b275a73777b7d7f34706a7d">[email protected]</a> <url> to an axios request if possible. ...

Leveraging functions with Ng-Repeat

I am currently dealing with two different arrays of objects. The first array contains a list of permissions groups, while the second array consists of user groups. My main goal is to compare the Group Owner (which is represented by ID 70) with the list of ...

Problem with loading messages in VueI18n locale

Utilizing the vueI18n package for language localization in our application, we fetch the locale messages object via an api call. Within our config file, we have specified the default language which is used to load the locale before the creation of app.vue. ...

Creating form inputs dynamically in HTML and then sending them to a PHP script programmatically

On my webpage, users can click a button to add new form inputs as needed. However, I'm running into an issue trying to access these new inputs in the PHP file where I submit the data. Here is the code snippet for the button within the form on the pag ...

Is it possible to reduce a field value in firestore after successfully submitting a form?

I have a variety of items retrieved from firestore: availability : true stocks: 100 item: item1 https://i.stack.imgur.com/hrfDu.png I am interested in reducing the stocks after submitting a form. I used the where() method to check if the selected item m ...

Is there an alternative method to incorporate the 'environment.ts' file into a JSON file?

In my Angular application, I need to import assets based on the env configuration. I am attempting to extract the patch information from environment.ts and save it into my assets as a json file. However, I am unsure of the proper method to accomplish this. ...

Steps to retrieve data (token) from developer tools and incorporate it into a fetch Post request

Is there a simple way to extract data using dev tools and insert it into a fetch request? I am trying to make a POST request through the console, but I am struggling to correctly copy a token. I attempted to use querySelector but instead of finding the t ...

The issue persists as the Ajax request continues to return false

I have created a verification program using an ajax request: $("#codeSbmt").click(function(){ var $input = $("#fourDigit"); codeCheck("codeTest.php?q=" + $input); }) function codeCheck(url) { var xh ...

Significant delay in processing second jQuery Ajax call

Attempting a simple Ajax call with page content refresh using the following code snippet: $("a.ajaxify-watched").bind("click", function(event) { $item = $(this); $.ajax({ url: $(this).attr("href"), global: false, type: "G ...

Express is throwing an error indicating that the specified route cannot be found. Is there a way to dynamically

After dynamically adding routes and sending a post request through Postman, I encountered a "not found" error message. In the app, a 404 is logged next to the endpoint, indicating that Express cannot locate it. However, I know that I added the router dynam ...

Is it possible for changes made to an object in a child component to be reflected in the parent component

When passing an object to the child component using , how can we ensure that changes made to a specific property in the object within the child component are visible in the parent component? In my understanding, changes would not be reflected if we were p ...

What is the best way to verify the presence of a value in an SQL column?

I need to check if a value exists in a column. If the value already exists, I do not want to insert it into the table. However, if it does not exist, then I want to add new data. Unfortunately, my attempted solution hasn't been successful. You can fi ...

a reliable approach to gain entry to properties that do not exist

Utilizing Google Apps Script along with JavaScript code, I am leveraging UrlFetchApp to retrieve data from the Stripe API in order to fetch an invoice. Subsequently, the Script processes the retrieved data and updates a Google Sheet template with it. An i ...

Laravel's Vue component is experiencing issues with loading Axios response data

Hey everyone, I hope you're all doing well! I'm facing a bit of an issue with passing data from an axios response to the data property. The data is successfully fetched from the database and displayed in the console, but when I try to display it ...