Home > Mobile >  Filtering an object in typescript
Filtering an object in typescript

Time:11-14

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?

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. ‏

CodePudding user response:

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

  • Related