Strategies for constructing or refining an object array with specified key and value from within a typescript object array

Is there a way to extract specific keys and values from an object array by filtering out those with "id" and "level" in TypeScript? Let's break it down with an example:

The original object array is structured like this:

[{
  op: AND
  id:0
  level: 0
  predicates: [{
    op: EQ
    field: abc,
    id:1.1
    level: 1
  }, {
    op "OR"
    id:1.2
    level: 1
    predicates: [{
      op: NEQ
      id:2.1
      level: 2
      field: 12312
    },
    {
      op: AND
      id: 2.2
      level:2
      predicates: [
        {
          op: EQ
          field: abc,
          id:3.1
          level: 3
        }
      ]
    }]
  }]
}]

The desired output is:

[{
  op:AND

  predicates: [{
    op: EQ
    field: abc,

  }, {
    op "OR"

    predicates: [{
      op: NEQ

      field: 12312
    },
    {
      op: AND

      predicates: [
        {
          op: EQ
          field: abc,

        }
      ]
    }]
  }]
}]

By comparing the original (input) object array with our desired output, we can observe that all key-value pairs with "id" and "level" have been filtered out.

What is the purpose of this task?

Since the original (input) object is dynamic and its structure may vary, implementing a recursive method can be challenging. Hence, I am seeking a more efficient solution using TypeScript.

Answer №1

It seems like the question is a bit unclear based on the information provided below.

The structure may not always be consistent with the example given, which can make writing a recursive method challenging.

Could you provide more details about your specific use case and what you are aiming to accomplish?

If you simply need to work with your data collection, consider converting it to a Modal class using a library like class-transformer. This way, you can easily iterate through your class objects as needed.

 import { plainToClass } from 'class-transformer';
 const data = plainToClass(Person, parsedData as Object[]); // parsed data could be JSON or an API response, while Person represents your Modal class

Once you have converted your data, you can perform various operations such as sorting, filtering, and displaying based on the properties of your modal class, tailoring them to meet your specific requirements.

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

From broad to specialized category

My goal is to inherit a class and modify its dynamic type to collapse into a specific type (from T to Person). In order to demonstrate this, I have created some dummy classes: class Person { constructor(public name: string){} } class Base<T> { ...

Navigating through nested JSON objects in React to display data effectively

I have been struggling for hours to find a solution to this problem with no success. I really need your assistance. The task at hand involves looping through a JSON file and creating a user interface that consists of multiple columns, each containing vari ...

Unable to verify Angular 5 identity

After authentication, the application should redirect to another page. However, despite successful authentication according to the logs, the redirection does not occur. What could be causing this issue? auth.guard.ts: import { Injectable } from &apos ...

Issue encountered while trying to import jszip in Angular project

Encountering an error when trying to import jszip into my TypeScript component file. After successfully running npm install jszip and confirming the package is properly installed, I proceed to import it using: import * as JSZip from 'jszip'; Th ...

"Unlocking the potential of pointer arrays in the C programming language

Working within an eclipse environment with C, I have a straightforward struct that defines essential elements for my project: typedef struct adc adc_def; struct adc { SPI_HandleTypeDef *spi_hal; //Address of the slave to communicate w ...

Searching live with array in PHP

I am currently implementing a live search feature on my website using AJAX and PHP. Despite trying to use XML, I didn't find it suitable for my needs. I prefer updating search results easily, which is more manageable with a database. Here's the ...

Using an array along with a function object: a step-by-step guide

Looking to create a versatile C++ algorithm that can operate on a variety of containers like sets, lists, maps, vectors, and more. The algorithm will take a container and a function object as parameters to evaluate specific conditions. The goal of this al ...

Tips for displaying complete object in mocha diff during assertion errors

While working on a unit test case created with mocha and chai's expect, I encountered a scenario where I needed to deeply compare an array of value objects to the parsed content of a JSON file. The record object I am dealing with has approximately 20 ...

Setting default values for HOCs in React

If I have a higher order component structure like this: interface MyHOCInterface { title: string } export function wrapMyHoc<T extends MyHOCInterface>( Component: React.ComponentType<T>,) { return class extends React.Component<T> { ...

Explain a TypeScript function that takes an object as input and returns a new object with only the

Check Playground What's the best way to define a type for this specific function? The inputObject should contain all keys from the enablePropertiesArray and may have additional ones. The function is expected to return a copy of the inputObject, inclu ...

What is the most effective method of utilizing union or extend in TypeScript when faced with a comparable scenario?

I have a function that takes in two different types of objects: const canBeGenericOrDefaultData = { id: 123, pointData: { square: 'x145', triangle: 'y145' } } function submitHandler(canBeGenericOrDefaultData: AllTheDatas | G ...

Having difficulties generating ngc and tsc AOT ES5 compatible code

I've explored various options before seeking help here. I have an angular2 library that has been AOT compiled using ngc. Currently, I am not using webpack and solely relying on plain npm scripts. Below is the tsconfig file being utilized: { "comp ...

Inspecting tRPC routing for examination purposes

Introduction Within my API, it is essential to authenticate the caller following input validation. The authorization for certain endpoints relies on details provided in the input parameters, such as the specific server-side resource being accessed and the ...

Encountered an error while trying to update information in Angular

I have been working on a project involving a .NET Core Web API and Angular 11 (Full-Stack) project. I have successfully managed to add data to the database in my back-end, but I am encountering an issue when trying to update the data. Below is a snippet o ...

Learn how to skip creating the tsconfig.json file in Next.js

I have structured my next.js project like this: site │ package.json │ tsconfig.json │ tslint.json │ yarn.lock │ ├─dist │ index.js │ └─src index.ts However, every time I run next build src, a new tsconfig.json file is ...

Average value for each individual channel in a collection of images

I am seeking to calculate the mean value for each RGB channel across an entire dataset stored in a numpy array. I am aware that this can be achieved using np.mean and I am familiar with its basic usage. np.mean(arr, axis=(??)) However, since the array con ...

Is the program's response is null upon execution?

After developing a program and ironing out run-time errors, I encountered a new issue - the program is not displaying any output when executed. The main functionality of the program involves merging data from two files to create a list of classes with cor ...

Processing Arrays in C++

I'm struggling to debug this code that is supposed to read two columns from a file. The goal is to add the values in the second column to an existing total for each department listed in the first column. It seems like there might be an issue with the ...

How can the Calendar Ant Design showcase events that have fluctuating dates?

Seeking a solution to display events on an Ant Design Calendar using dateCellRender with dates stored in a variable object. Here's an example of the object: [ { "id": 1, "content": "Example", & ...

How can I pull all data from an array using MongoDB query?

I have multiple arrays, but I am only interested in extracting the content related to "PIZZAS." Can anyone advise me on the appropriate query to achieve this? https://i.stack.imgur.com/wHolE.png ...