Deleting elements from an array of objects in Angular Would you like help with

I have a JSON structure and I need to remove the entire StartGeotag object from the array.

[{
    CreatedDate: "2022-02-17T10:30:07.0442288Z"
    DeletedDate: null ProjectId: "05b76d03-8c4b-47f4-7c20-08d9e2990812"
    StartGeotag: {
        Type: 'Point',
        Latitude: '33.6607231',
        CreatedDate: '2022-02- :34:46.5389961Z'
    }
    StartTime: "2022-02-17T10:30:05.828Z"
}]

Answer №1

Utilizing the ES6 Spread Operator allows you to accomplish this task: To remove specific keys, simply provide those keys as arguments, and ...rest will return the remaining arguments.

var data = [{ CreatedDate: "2022-02-17T10:30:07.0442288Z", 
DeletedDate: null, 
ProjectId: "05b76d03-8c4b-47f4-7c20-08d9e2990812",
StartGeotag: {Type: 'Point', Latitude: '33.6607231', 
CreatedDate: '2022-02- :34:46.5389961Z'},
StartTime: "2022-02-17T10:30:05.828Z"}]

const res= data.map(({StartGeotag, ...rest}) => ({...rest})); 

console.log(res);

Answer №2

To utilize object destructuring assignment introduced in ES6, you can destructure objects easily.

See the code snippet below for a demonstration :

let jsonObj = [{
    CreatedDate: "2022-02-17T10:30:07.0442288Z",
    DeletedDate: null,
    ProjectId: "05b76d03-8c4b-47f4-7c20-08d9e2990812",
    StartGeotag: {
        Type: 'Point',
        Latitude: '33.6607231',
        CreatedDate: '2022-02- :34:46.5389961Z'
    },
    StartTime: "2022-02-17T10:30:05.828Z"
}];

let res = jsonObj.map(({StartGeotag, ...remainingItems}) => remainingItems)

console.log(res);

Answer №3

To easily extract specific data from your object, you can use object destructuring.

[{ CreatedDate: "2022-02-17T10:30:07.0442288Z" , DeletedDate: null, ProjectId: "05b76d03-8c4b-47f4-7c20-08d9e2990812",
    StartGeotag: {Type: 'Point', Latitude: '33.6607231', CreatedDate: '2022-02- :34:46.5389961Z'},
    StartTime: "2022-02-17T10:30:05.828Z"}].map(({StartGeotag, ...item}) => item)

If you specifically need only the StartGeoTag object, you can achieve that like so:

 [{ CreatedDate: "2022-02-17T10:30:07.0442288Z" , DeletedDate: null, ProjectId: "05b76d03-8c4b-47f4-7c20-08d9e2990812",
    StartGeotag: {Type: 'Point', Latitude: '33.6607231', CreatedDate: '2022-02- :34:46.5389961Z'},
    StartTime: "2022-02-17T10:30:05.828Z"}].map(({StartGeotag}) => StartGeotag)

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

Utilizing Typescript for constructor parameter assignments

Within my codebase, there exists an interface: export interface IFieldValue { name: string; value: string; } This interface is implemented by a class named Person: class Person implements IFieldValue{ name: string; value: string; const ...

Repairing unclean JSON requests with Node.js

My node.js API is receiving JSON data that is usually good, but sometimes contains bad characters at the end. { "test": "test" }��� I am trying to find a way to intercept this data before it reaches the BodyParser and causes errors. I attempted t ...

Intellisense missing in VSCode for Angular and typings

Attempting to start a new project using Angular 1.5.5 and looking to incorporate TypeScript into my coding process within Visual Studio Code. I have included the necessary typings for Angular in my project: typings install angular --save --ambient I&ap ...

The function getServerSideProps does not return any value

I'm a beginner with Next.js and I'm currently using getServerSideProps to retrieve an array of objects. This array is fetched from a backend API by utilizing the page parameters as explained in the dynamic routes documentation: https://nextjs.org ...

Having trouble parsing JSON data? Wondering how to troubleshoot this issue?

I need help with an API call that is causing an error. The code for my getData() function is as follows: func getData(from url: String) { URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in guard let da ...

Is there a way to sort through objects in a JSON file using two shared values? Specifically, I'm looking to filter the JSON objects based on both common x and y values

Given a JSON file, I am looking to group objects based on common x and y values. Essentially, I want to group together objects that share the same x and y properties. Here is an example of the JSON data: let data = [{ "x": "0", "y& ...

Angular directive for converting fields to title case

I have created a custom directive to transform all table column data into titlecase. The directive I implemented is designed to achieve this transformation, keeping in mind that pipes are not being counted: @Directive({ selector: '[appTitleCase]' ...

The function in Swift that allows for escaping

I recently tried following a tutorial on YouTube that demonstrated how to retrieve data from a JSON file. However, I found some of the steps confusing. Here's the specific function being used: func fetchJSONData (completion: @escaping () -> ()) { ...

Tips for instructing kysely key-gen to utilize basic data types for mapping database tables

While using the kysely-codegen tool to automatically create all models from my database, I have noticed a peculiar behavior. It seems to be adding a Generated type to every field instead of generating only number or boolean. Any idea why this is happening? ...

Encountering a Spring Boot 404 error when deploying static content as a jar file

Utilizing a Spring Boot application alongside an Angular JS project housed in separate modules raises some concerns. The Angular JS files, located within the 'dist' folder, have been converted into jar files and integrated into the Spring Boot se ...

Failure to Execute Angular HttpClient Request

I'm facing an issue with firing the HttpClient request. It seems like there might be a problem with importing or providing, but I can't pinpoint where it is exactly. The API works fine, but the call never goes through. Here are the environment/v ...

When attempting to compile my Angular project using the command ng build --prod, I encountered a server error stating that the document

Everything was running smoothly when I was working on my local machine, but once I uploaded the files generated from ng build --prod to the server, a problem arose. Now, whenever I try to route via a button in my components, an error appears. When I clic ...

What is the best way to retrieve data from a JSON object?

Can the status variable be used as a JSON object? What is the method to access the values of action_success and newIndex within the status object? Server: [HttpPost] public ActionResult UploadFiles() { // save file.. return Json(new { action_suc ...

Is there any value to be retrieved from the URL?

One of my questions involves handling a URL from YouTube, specifically extracting the video ID. $url='http://www.youtube.com/watch?v=H_IkPia6eBA&'; My goal is to simply retrieve the video ID value, such as in this case: $newstring='H_ ...

Retrieving information from a .json file using TypeScript

I am facing an issue with my Angular application. I have successfully loaded a .json file into the application, but getting stuck on accessing the data within the file. I previously asked about this problem but realized that I need help in specifically und ...

What is the correct way to properly parse JSON attributes containing slashes?

I have 2 Custom Interfaces: DataModel.ts export interface Entry{ id: number, content: Note } export interface Note{ message: string } These interfaces are utilized in typing the HttpClient get request to fetch an array of Entries: DataService.ts getE ...

Transform the CSS to a Mat-Form-Field containing a search box within it

I am currently working with Angular, Angular Material, and SCSS for my project. Here is the front-end code snippet that I am using: <mat-form-field class="custom-form-field" style="padding-top: 5px;padding-bottom: 0px; line-height: 0px; ...

Converting hash values from strings to arrays in Ruby: A simple guide

I am interested in converting the values in a Ruby hash from Strings to Integers. I have been exploring ways to achieve this, but have not found a simple solution that doesn't involve extra steps like converting the hash to an array first. Is there ...

Typescript defines types for parameters used in callbacks for an event bus

Encountering a TypeScript error with our custom event bus: TS2345: Argument of type 'unknown' is not assignable to parameter of type 'AccountInfo | undefined'. Type 'unknown The event bus utilizes unknown[] as an argument for ca ...

Confirming if a JSON is valid in the C programming language

After hours of searching on Google, I am eager to find a way to programmatically verify if the string provided below is valid JSON using C. Is there any solution available? (I am currently relying on the json-c library.) char * jsonString = "{ ...