Remove a particular property from JSON data. I only want to remove key-value pairs where the Category is set to null, not all pairs

I have a JSON dataset and I need to eliminate any categories with null values from it.

Specifically, I want to keep "Category":"start" intact but remove any instances of "Category":null.
I've come across solutions that achieve this but they end up deleting all categories, including "Category":"start", which is not what I'm looking for.

"First": [{
        "category": "Start",
        "name": "Start",
        "key": 1,
        "lastname": "xyz"
    }, {
        "category": null,
        "text": "Step",
        "key": 2,
        "lastname": "xyz"
    }, {
        "category": null,
        "text": "Condition",
        "key": 3,
        "loc": "xyz"
    }

Answer №1

An efficient way to achieve this is by utilizing the map() function along with parameter destructuring.

const items = [{ "category": "Start", "name": "Start", "key": 1, "lastname": "xyz" }, { "category": null, "text": "Step", "key": 2, "lastname": "xyz" }, { "category": null, "text": "Condition", "key": 3, "loc": "xyz" }]
const result = items.map(({category,...rest}) => category === null ? {...rest} : {category,...rest})
console.log(result)

Answer №2

To achieve the desired output, refer to the code snippet provided below.

const array = [{
  "category": "Start",
  "name": "Start",
  "key": 1,
  "lastname": "xyz"
}, {
  "category": null,
  "text": "Step",
  "key": 2,
  "lastname": "xyz"
}, {
  "category": null,
  "text": "Condition",
  "key": 3,
  "loc": "xyz"
}];

const list = array.map(item => {
  let object = item;
  [undefined, null].includes(object.category) && delete object.category;
  return object;
});

console.log(list);

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

Using the fs module in a React Native application

Currently, I am facing a challenge in importing TinyDB into an expo project. The issue lies with fs, as Expo does not support it due to its third-party file system nature. I attempted using react-native-fs as an alternative but I am unsure about how to pr ...

No matter how many times I modified the code in the ReactDOM.render() method within my index.js file, the end result remained unchanged

When I ran npx create-react-app my-app, and then proceeded to cd my-app and npm start, a browser opened on localhost:3000. While looking at the index.js file, I noticed that the ReactDOM.render() method included the following code: ReactDOM.render( <Rea ...

Using Three.js to establish coplanarity of points and project coplanar points onto the XY plane

Looking to determine if a set of Vector3 points are co-planar in a specific plane and then project them onto the XY plane while maintaining their scale. Any suggestions on how to accomplish this efficiently using three.js? ...

Developing object in place of an array (JavaScript)

After receiving data from the back-end, I handle it within a function: getAgentSuggestionById(agentId) { this._agentsService.getAgentSuggestionById(agentId).subscribe(result =>{ this.agent = result.items.map(e => {let obj = {name: e.name ...

Authentication for REST API using JavaScript in the web browser

Currently, I am experimenting with creating a stateless, REST-based API that I intend to use from multiple sources, one of which will be a single-page Javascript web application. The goal is to have a unified API for different clients, even those developed ...

Tips for utilizing postman filters within django applications

I need help implementing a POST method with a JSON object in Django. I have the code below but I'm not sure how to apply this POST method: POST Method Example from Postman { "filter": { "filters": [ { "field": "CreatedOn", ...

Display a featherlightbox popup when the page loads

Well, here's the deal. I don't have any experience with wordpress php coding at all, but I can make basic adjustments using the wordpress admin. Now I've run into an issue. I tried to use the feather lightbox js, and below is a snippet of co ...

Is there a way to retrieve the request object within loopback operational hooks?

I am currently utilizing loopback 3.x and have created an Access Hook within my code. My goal is to include a condition based on the User Agent. Specifically, I am looking to access Request > Headers > User-Agent. Is it feasible to retrieve this in ...

Encountering blank values while retrieving JSON data in Objective C

Having trouble parsing JSON data in Objective C where lower level object values are returning null for userid, FirstName, and LastName. Here's the complete JSON: { "members" : [ {"member" : {"userid":"1","FirstName":"ramesh","LastName":" ...

Guide on designing a navigation list with unique buttons that alter the displayed content based on the selected button

As a newcomer to the world of programming, I have a basic inquiry that I hope you can assist me with. I have successfully created a navigation list, but I am struggling to make it functional. For instance, if my navlist consists of 3 buttons: Home, About, ...

Invoke a web service upon the loading of a page within the scope of an AngularJS Framework

My webservice returns a JsonObject and I am calling it in JS using Ajax as shown below : $scope.init = function () { $.ajax({ type: 'GET', timeout: 1000000000, url: serv ...

The Ajax request encountered a syntax error due to an unexpected token '<'

I have searched the forum extensively for similar questions, but unfortunately haven't found a suitable answer for my specific problem. In my jQuery code, I am attempting to make an AJAX call using the following snippet: function submitForm(formData) ...

AngularJS - A pagination demonstration incorporating intelligent logic inspired by Google's approach

I struggled to implement a paging solution that I came across online. The issue seems to be related to the timing of function calls, specifically how the setPage function is triggered before all data is retrieved, causing it to not properly determine the t ...

What is the best way to extract data from a JSON object that is passed as a string parameter?

My JSON data has the following structure: { "country": "London", "name": "Bookstore", "books": [ { "title": "book1", "description": ...

Special symbols used in a URL - searching for the uppercase Ø

Here is the URL I am working with: my.website/?param1=Økonomi&param2=Penger In order to retrieve the parameter values, I am using the following function: function extractParamValue(url, key) { var parameters = {}; var urlParams = url.split( ...

Removing classes from multiple elements on hover and click in Vue 2

Can Vue be used to remove a class from an element? I am looking to remove the class when hovering over the element, and then add it back once the mouse is no longer on the text. Additionally, I want the class to be removed when the element is clicked. He ...

Retrieve the information from a JSON reply

Click here for the API response The JSON response includes a "stats" field with RAP value of "270,924". I'm looking to extract the RAP value from the response, which is 270,924. Can someone please assist me? Thank you. ...

I'm noticing a significant slowdown on my webpage due to a high volume of href URLs in my HTML script. What steps can I take to resolve this issue

As a newcomer to scripting, I spent countless hours researching to find a solution but couldn't crack it. I implemented a jquery image search script that functioned smoothly with all components embedded in a single HTML code. However, things took a t ...

Using JavaScript, capture and rearrange the colors of the store's section. JS

I'm facing a challenge with creating dynamically colored divs based on different sections in the background. While I can achieve this using CSS, I wonder if there's a more efficient way to handle the colors with JavaScript. In my current setup, ...

Spreading activity among react elements

Recently, I decided to repurpose a login/register modal by incorporating a Modal to manage its visibility. In the index.js file, I made the following changes: const Form = ({ initialState = STATE_SIGN_UP, showPopUp = STATE_HIDE }) => { const [mode, t ...