exclude properties with different values in an array

const myArray = [ { status: null }, { rooms: 2 }, { bathrooms: 3 }, { vaccum: null } ]

I am working with the above array and I need to filter out any objects that have a property value of null, regardless of what the property is.

Because the properties in the objects are not consistent, using this code won't work:

const filteredArray = myArray.filter(property => {
    property.status == null
})

Answer №1

A way to achieve this is by filtering based on the object values and checking if any of them are null or not

const myArray = [{
  status: null
}, {
  rooms: 2
}, {
  bathrooms: 3
}, {
  vaccum: null
}];

let result = myArray.filter( o => {
  return !Object.values(o).includes(null)
});

console.log(result)

Object.values may not be supported in all browsers, but a simple loop can be used for backwards compatibility

const myArray = [{
  status: null
}, {
  rooms: 2
}, {
  bathrooms: 3
}, {
  vaccum: null
}];

var result = myArray.filter( o => {
  for (key in o) {
  if ( o[key] === null ) return false;
  }
  return true
});

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

Grouping results by field data is a common practice in MYSQL and PHP

My task involves generating reports based on data extracted from a log within the database structured as follows: id |    student    |    type     |    marks 1       23494   &n ...

Steps to extract a portion of a URL and display it in the "src" attribute

Here is the URL: My Code: (please refer to the JS comments for instructions on how to complete steps 2 and 3) <script> function mdlbox() { //step 1: Show the modal box var y = document.getElementsByClassName('modalDialog'); ...

Guide on finding the index of the clicked element based on its class

Is there a way to get the index of the clicked element within its list as well as in its .blue class? I have successfully obtained the index within its list, but I am struggling to figure out how to get the index within its .blue class. For instance, when ...

The outcome of the Next.js query has not been explicitly provided

Currently, I am learning Next.js (TypeScript) and I have created the function getServerSideProps({query}) in detail/[slug].tsx. However, the query does not provide the expected result in the URL. For example, when the URL is localhost:3000/detail/samsung- ...

Store the output of a mysql query in a variable for future reference

As I work on developing a backend API for a private message system, I have encountered a challenge with one of my functions. The issue arises when I attempt to store the result of an asynchronous call in a variable for future use. Here is the code snippet ...

Creating wrapped text in Three.js

Is there a way to wrap a Text3d object around a 3D or 2D Path in Three.js? I have looked into some tutorials for r.49 of Three.js and it appears that the current version does not have support for this feature. While I am able to create the text and extru ...

Tips for revealing a link after the user clicks on the submit button

I am looking to set up a webpage that will display a hyperlink after the submit button is clicked. For example, 1) wedding 2) engagement 3) birthday All three items are checkbox buttons and there is a textbox to input the budget. After clicking the sub ...

When the pop-up remains open between button presses, the Javascript window[] variable is undefined

Here is an example code snippet that utilizes Javascript to open a pop-up window: If the pop-up is closed before pressing another button, the data is successfully sent and can be accessed inside the pop-up's javascript code using the following syntax ...

Node.js retrieves a single row from a JSON array with two dimensions

I am working with a two-dimensional JSON array and I am able to retrieve data from the first dimension using data["dimension-1"], but I am struggling to access data from the second dimension using data["dimension-1"]["dimension-2"]. What is the correct m ...

What is the best way to reference a component from another component in a React application?

I've been utilizing the react-notification-system library in my project, and here's a snippet of how I've incorporated it into my code. import React from 'react'; import Notification from 'react-notification-system'; cl ...

Encountering an issue with Invariant Violation when attempting to retrieve a frame that is outside of the specified

I have created a VenueList component for my React Native app. I want to utilize the FlatList component to display a list of venues, but I keep encountering an error message that says "Invariant Violation tried to get frame out of range index" (refer to the ...

Utilizing autosuggest in combination with jQuery ajax consistently generates suggestions with a delay of 1 keystroke

I'm currently working on creating an autosuggest feature for a search box, but I've run into a problem. The suggestions that are displayed don't seem to match the current keystroke input (they keep showing suggestions based on the previous k ...

Troubleshooting 404 errors on Firebase hosting with dynamic routes

My next.js app is deployed on firebase. The app has been deployed and in use for some time now. I created a dynamic route page Redeployed the app While I could access the dynamic page via a router, I encountered a 404 page not found error upon refreshing ...

After the execution of the script by V8, which phase will be initiated first?

Scenario // test.js setTimeout(() => console.log('hello'), 0) setImmediate(() => console.log('world')) Simply execute node test.js using node v12.12.12 on an Intel MacBook Pro. The output may vary: hello world Sometimes it is: ...

Sending a function as a callback to children components in order to update a specific child component

I am currently working on developing a Navbar component that undergoes slight changes when a user logs in through a SignIn component. Here is an overview of how my application is structured: Initially, I have defined a state in the App component where aut ...

Seamless animation when collapsing element using angular and css exclusively

I am attempting to incorporate a collapsible feature into my application. Rather than relying on external libraries like jQuery, I want to exclusively utilize Angular. For demonstration purposes, I have created a very basic example here: http://jsfiddle.n ...

In Javascript, set the index variable of the respected duplicate value to true in the array

I am seeking a solution for identifying duplicate values within an array of objects. My goal is to set the value of a specific index variable to true if that index contains a duplicate value. For example, consider the following array of objects: let ...

Interact with a webpage element that is nested within another element using JavaScript

<div _ngcontent-tbs-c102="" class="tbis-div"><div _ngcontent-tbs-c102="" tabindex="0" class="hidden-xs" aria-label="2. Result for and displayed for your query"><span _ngcontent ...

Ways to structure a MySQL query based on date range using JavaScript

How can I retrieve data between two dates using express js in JavaScript? The query I am using is shown below: period = `created_datetime BETWEEN ` + fromdate + ` AND ` + todate; However, I am encountering an error as follows: You have an error in your S ...

The height map method for plane displacement is experiencing issues

The heightmap I have selected: Scene without grass.jpg map : Scene with grass.jpg map: https://i.sstatic.net/q6ScO.png import * as THREE from 'three'; import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'; import ...