Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filtering an object in typescript

I have a json file with the data below and stored as file.json:

{
    "player_hub": {
      "boundary": { "x": 100, "y": 100, "z": 100 },
      "contents": [
        {
          "type": "space",
          "quantity": { "amount": 1, "variance": 5 },
          "position": "fixed"
        },
        {
          "type": "terrain",
          "quantity": { "amount": 1, "variance": 5 },
          "position": "grid"
        }
      ]
    },
    "retail": {
      "boundary": { "x": 100, "y": 100, "z": 100 },
      "contents": [
        {
          "type": "space",
          "quantity": { "amount": 1, "variance": 5 },
          "position": "fixed"
        },
        {
          "type": "terrain",
          "quantity": { "amount": 1, "variance": 5 },
          "position": "grid"
        }
      ]
    }
  }

I imported the json file as below in main.ts:

import { Suspense } from 'react';
import dataTypes from 'file.json';

const Main: React.FC<BusinessType> = ({ type }) => {
    
    // I wan to filter the dataTypes object when the key equal to `player_hub`

    return (
    //   something
    );
  };
  
  export default Main;

How can I filter the dataTypes object when the key equal to player_hub?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried to use javascript filtering but didn’t work

  Object.fromEntries(Object.entries(dataTypes).filter(type))‏


I want to filter the dataTypes object when the key equal to player_hub in main.ts file.

>Solution :

I found a good resource for your question, I think this link explained everything you want.

https://www.steveruiz.me/posts/how-to-filter-an-object

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading