Tips for Dealing with Empty Rows in Arrays

How can I remove rows from an array in Alasql where all key values are null?

Here is the array data:

[
0:{Name:"ABC1",No:5,BalanceDue:5000,Notes1:null,Notes2:null,CurrencyId:"2",Date:"06/01/2018"}
1:{Name:"ABC2",No:6,BalanceDue:6000,Notes1:null,Notes2:null,CurrencyId:"3",Date:"05/02/2018"}
2:{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}
3:{Name:"ABC3",No:7,BalanceDue:7700,Notes1:null,Notes2:null,CurrencyId:"8",Date:"15/02/2018"}
4:{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}
5:{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}
6:{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}
]

Desired result:

[
0:{Name:"ABC1",No:5,BalanceDue:5000,Notes1:null,Notes2:null,CurrencyId:"2",Date:"06/01/2018"}
1:{Name:"ABC2",No:6,BalanceDue:6000,Notes1:null,Notes2:null,CurrencyId:"3",Date:"05/02/2018"} 
2:{Name:"ABC3",No:7,BalanceDue:7700,Notes1:null,Notes2:null,CurrencyId:"8",Date:"15/02/2018"} 
]

Answer №1

To remove objects with all values as null, you can utilize the combination of array#filter and array#every.

var data = [{Name:"ABC1",No:5,BalanceDue:5000,Notes1:null,Notes2:null,CurrencyId:"2",Date:"06/01/2018"},{Name:"ABC2",No:6,BalanceDue:6000,Notes1:null,Notes2:null,CurrencyId:"3",Date:"05/02/2018"},{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null},{Name:"ABC3",No:7,BalanceDue:7700,Notes1:null,Notes2:null,CurrencyId:"8",Date:"15/02/2018"},{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null},{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null},{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}],
  result = data.filter(o => !Object.keys(o).every(k => !o[k]));
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

Creating interactive forms with Angular 9 using ngx-formly and arrays for dynamic form generation

Looking to implement: Dynamic Forms based on user-selected locale JSON using ngx-formly/material. How can I connect fields with my array of objects "this.fields2"? In my component.ts file, I have: model: any = {}; options: FormlyFormOptions = {}; fields: ...

Does one require the express.js framework in order to create a web application using nodeJS?

Exploring the creation of a basic web application using HTML, NodeJS, and Postgres. Can this be achieved without incorporating the ExpressJS framework? Seeking guidance on performing CRUD operations with NodeJs, Javascript, and Postgres sans ExpressJS. G ...

Is it secure to use ES6 in Typescript with nodejs/koa?

As I transition to using TypeScript in a Node.js/koa project, I came across the need to modify the .tsconfig file to target ES6. Otherwise, an error message will appear stating: Generators are only available when targeting ECMAScript 6 or higher. // index ...

Executing an observable only when a certain variable is in a specific state at the class level

Is there a way to conditionally return an observable in a clean manner, based on a predefined condition? Let's look at the scenario below: isEditing = false; // <---- Variable defined at class level return this.usersService.fetchInitialCreateDat ...

Storing a Vue/JS element reference in a constant using Typescript

In my template, I have one form element and one button element: <button type="submit" id="ms_sign_in_submit" ref="submitButton" class="btn btn-lg btn-primary w-100 mb-5"> </button> Wi ...

When using Material-UI with TypeScript, an error is thrown stating that global is not defined

After running the following commands: npm install --save react react-dom @material-ui/core npm install --save-dev webpack webpack-cli typescript ts-loader @types/react @types/react-dom I transpiled main.tsx: import * as React from "react"; import * as R ...

Choose the Angular 2 option

I am having an issue with the select option in my code. The person's gender property is assigned 'M' for male, but I need to allow users to change it to 'F' for female. Here is the HTML code snippet: <span > <span &g ...

React Material-UI - implementing custom colors in Alert component leads to error: "Cannot read properties of undefined (reading 'type')"

I've been working on customizing MUI components to match our company's design, but I've hit a roadblock. While defining my custom colors, I noticed that instead of "error" we have a color called "danger." I followed the guidelines in the do ...

Tips for navigating through a nested JSON object with loops

Is it possible to access the value of the Address object within an interface in Angular using *ngFor? export interface User { id: number; name: string; username: string; email: string; address: Address; } export interface Address { st ...

Does Typescript not provide typecasting for webviews?

Typescript in my project does not recognize webviews. An example is: const webview = <webview> document.getElementById("foo"); An error is thrown saying "cannot find name 'webview'". How can I fix this issue? It works fine with just javas ...

I'm stuck trying to figure out all the parameters for the MapsPage component in Angular 2

Currently, I am utilizing Angular2 with Ionic2 for my mobile app development. Everything was working flawlessly until I decided to incorporate a new module for Google Maps navigation. Specifically, I am using phonegap-launch-navigator for this purpose. The ...

Get the final element with a for loop in Angular 2

I am currently utilizing angular 2 in conjunction with mongoDb. The service I am employing is responsible for calling the API. Here is a snippet of my code: this.at represents a token, similar to fdfdsfdffsdf... This token serves as an authorization key ...

What is the best way to assign a TypeScript type as the object type within an array of objects sourced from a different interface?

I am working with a generated interface that looks like this: export interface StaticPageLeftMenuV1 { id: string status: 'draft' | 'published' environments: ('dev' | 'staging' | 'production')[] ...

Change Json data into a TypeScript class [ts]

I have the following JSON data: {"mapData":{"test":"success","publicKey":""},"statusCode":200,"message":null} How can I convert this JSON to a TypeScript class? The mapData contains anonymous values: It may be {"test":"success","publicKey":""} Or it m ...

When defining properties/data in Vue mixins, the properties/data of the mixin are not accessible

A vue mixin is being used to store information (referred as `world` in the example below) that needs to be accessed in multiple vue components without having to import it every time. Check out the code snippet: <template> <ol> <li> ...

Angular 5: Create a dropdown menu with image options

Currently, I am utilizing a select element within Angular 5 while incorporating bootstrap 3. This component seems to be from WebForms / Bootstrap. I am curious if it is feasible to add images to the various options, possibly through the use of CSS? ...

Ways to retrieve and upload a blob from a file object

After receiving a file object via an HTML input on-change event in my component.html: onFileSelected(event) { this.selectedFile = event.target.files[0]; } Now, I need to send a POST request to upload the image to the database. The database only acc ...

Using Observables in Angular 2 to send polling requests

I have the following AngularJS 2 code snippet for polling using GET requests: makeHtpGetRequest(){ let url ="http://bento/supervisor/info"; return Observable.interval(2000) .map(res => res.json()) //Error here ...

Can you explain the meaning of '<Hero[]>' in programming jargon?

Hello there! I am new to learning angular and typescript, and currently going through a tutorial at angular. However, I stumbled upon something that I find confusing. For example: 1. getHeroes(): Observable<Hero[]> { this.messageService.add(&ap ...

Connect Angular ngx-datatable accountid to a specific details page

My datatable is displaying static data with account numbers and other details, including a column for actions such as viewing a row. When I click on the button, an alert shows me the specific details. userdetails.component.ts rows: any = [ { id: 0 ...