Removing an item from a JSON list

Is there a way to remove a specific element (e.g. by using its id) from a JSON array?

A delete button can be used to achieve this task, passing the id as a parameter.

For instance, how would one go about deleting an item with an id of 2 :

Note: The id number 2 could be located at index position 10 or 20 within the array.

"id": 2, "date": "27 Mar 2018", "name": "name 2", "type": "meeting"

        var id = 1;
        var events = [{
                "id": id++,
                "date": "26 Mar 2018",
                "name": "name 1",
                "type": "task"
            },
            {
                "id": id++,
                "date": "27 Mar 2018",
                "name": "name 2",
                "type": "meeting"
            },
            {
                "id": id++,
                "date": "27 Mar 2018",
                "name": "name 3",
                "type": "meeting"
            }

        ];

        events.push({
            "id": id++,
            "date": "25 Mar 2018",
            "name": "name 4",
            "type": "meeting"
        });

Answer №1

To achieve the desired outcome, follow these steps:

let tasks = [{
                "id": 1,
                "date": "26 Mar 2018",
                "name": "name 1",
                "type": "task"
            },
            {
                "id": 10,
                "date": "27 Mar 2018",
                "name": "name 2",
                "type": "meeting"
            },
            {
                "id": 15,
                "date": "27 Mar 2018",
                "name": "name 3",
                "type": "meeting"
            }

        ];
        let entryToRemove = tasks.find(task => task.id === 10);
        tasks.splice(tasks.indexOf(entryToRemove), 1);
        console.log(tasks);
        

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

Activate a Dropdown Menu by Clicking in a React Application

I have a collapsible feature where you can click to expand or collapse a dropdown. Currently, the dropdown can only be clicked on where the radio button is located. I want the entire area to be clickable so that users can choose the dropdown by clicking an ...

`How can I activate a modal window when an option is chosen?`

Utilize the select tag and modal window tag for this example. <select id="selectBox"> <option value="0">Select</option> <option value="1">Yes</option> <option value="2">No</option> </select> <div id ...

A groundbreaking algorithm engineered to launch a sequence of distinct hyperlinks upon each button press

I have a unique script that allows me to open links randomly, but now I want to create a new script that opens links sequentially and goes back to the initial one. For example, clicking a button will open link1, then clicking it again will open link2, an ...

Utilizing ng-class with Boolean values in AngularJS

On my page, I have a pair of buttons that control the visibility of elements using ng-hide and ng-show. <a ng-click="showimage=true" ng-class="{ 'activated': showimage}" class="button">Images</a> <a ng-click="showimage=false" ng-c ...

When running npm install -g, the package is being installed into a global directory on

For a considerable amount of time, I have been working with node and npm. Now, as I embark on a more extensive project using these tools, I've encountered an issue. Whenever I execute 'sudo npm install -g', the installation is directed to &a ...

Converting UTC Date Time to Full Date Using ES6

Is there a way to transform the date 2021-01-10 12:47:29 UTC into January 10, 2021? I've been attempting to achieve this using moment.js code below, but it seems to be functioning in all browsers except Safari. {moment(video?.createdAt).format(' ...

Parsing PHP array using JavaScript

$namesSelect = "SELECT username FROM users"; $names = mysql_query($namesSelect); $nameCheck = mysql_fetch_array($names) This code snippet retrieves all usernames from the users table and stores them in the $names array. I am now faced with the task of ...

Is it possible to terminate an active server process triggered by an HTTP post request in Node.js prior to returning a response?

I developed an application where I utilized Ajax to make calls to a Node server. The issue is that even if the user navigates to another page, the server persists in processing the initial request made by the Ajax call. It then proceeds to process the new ...

Tips for solving the problem of page navigation using jquery-mobile

I have a jquery-mobile application with two actual HTML pages, but many more data-role="page" divs. The issue arises when transitioning from the first actual HTML page to the next one as the navigation stops working. I am not using any custom JavaScript, j ...

Extracting a precise 14-digit numerical value from a JSON file using Java

Working with a JSON source obtained from an API at my workplace, I am tasked with extracting the balance from the JSON response. This value is crucial and usually appears in the format of xx.xxxxxxxxxxxxxx, which consists of two digits followed by up to 14 ...

I am facing an issue where this loop is terminating after finding just one match. How can I modify it to return

I am currently working with an array that compares two arrays and identifies matches. The issue is that it only identifies one match before completing the process. I would like it to identify all matches instead. Can anyone explain why this is happening? ...

Monochrome Effect Triggered by Cursor Hover

I'm attempting to use Javascript with HTML5 canvas to convert an image to greyscale, but I seem to be missing something in my code. Can anyone spot the error? I feel like I'm very close! function grayscaleConversion(str) { // Access the Canv ...

Preventing long int types from being stored as strings in IndexedDB

The behavior of IndexedDB is causing some unexpected results. When attempting to store a long integer number, it is being stored as a string. This can cause issues with indexing and sorting the data. For instance: const data: { id: string, dateCreated ...

Tips for retrieving a substantial JSON object using the Firefox developer console

Currently, I am developing a web application that generates large JSON objects. To keep track of these JSONs, I use console.log and now I am in search of a method to extract them and save them into a text file. The main challenge lies in handling the size ...

Unexpected results were generated by json-lib when handling a string that seems to be in array form

Here is a snippet of code that I am working with: import net.sf.json.*;--just so you know what the library is ... JSONArray a = new JSONArray(); JSONObject p = new JSONObject(); p.put("some_attribute1","some normal string"); p.put("some_attribute2","[3som ...

Combine the elements of two arrays of varying sizes to create a single array

Is it possible mathematically? That's what I'm not quite sure of, but I thought I'd ask anyway. The challenge I'm facing involves two arrays with sizes (50,1) and (693,1). My goal is to add the values from these arrays together each ti ...

Navigating through React Native with TypeScript can be made easier by using the proper method to pass parameters to the NavigationDialog function

How can I effectively pass the parameters to the NavigationDialog function for flexible usage? I attempted to pass the parameters in my code, but it seems like there might be an issue with the isVisible parameter. import React, { useState } from 'rea ...

Generate AngularJS ng components programmatically with JavaScript

I am curious about how to generate the following using AngularJS in JavaScript: <input type="text" ng-model="TheText">{{TheText}} For instance, utilizing a method like this: <!DOCTYPE html> <html> <head> </head> ...

Loading 3D Models with Three.js and the Canvas Renderer

I recently developed a project using WebGLRenderer, but the client has now requested that we switch to Canvas instead. I am currently facing an issue where a JSON model is not fully visible when loaded, despite basic geometry displaying correctly. Any sugg ...

Leveraging code behind and serialized JSON in an ASP.NET web application

I've recently created an array using a LINQ query: var aTimeResultQuery = (from fct in context.fct_testautomation join dr in context.dim_driver on fct.driver_key equals dr.driver_key join tc in context.dim_test_case on fct.test_case_ ...