TypeScript: how to find an object in an array and return that specific object?

Nr.

I'm trying to find an object in an array and return that specific object. I'm new to typescript, so I tried to solve it this way and I'm not sure why it's not working.

Edited: My code is not even being complied! I'm getting the error below:

TypeScript error in /Users/.../api/VaccineDataApi.tsx(64,14): Object is possibly 'undefined'. TS2532

import React, { useEffect, useState } from 'react';
import axios from 'axios';

type VaccineDataState = {
    Date: Date,
    Location: string, //Location is state Abbreviations like 'WA'
    LongName: string,
    Doses_Distributed: number,
}

const VaccineData = () => {
    const [vaccine, SetVaccine] = useState<VaccineDataState[]>([])
    const [errorMessage,SetErrorMessage] = useState(null)

    useEffect(() => {
        axios.get("https://localhost:3000")
        .then((response) => {
            const apiVaccineData = response.data.vaccination_data;
            SetVaccine(apiVaccineData);
        })
        .catch((error) => {
            SetErrorMessage(error.message);
            console.log(errorMessage);
        })
    }, []);

  
    let vaccineData = vaccine.find(state => state.Location === 'WA');

    return(
        <div>
            <p>test</p>
            {vaccineData.Location}
        </div>
    )
    
};

export default VaccineData;

tmwilliamlin168

VaccineData refers to the functional component. You probably wanted vaccineData.Location instead of VaccineData.Location.

Edit: To fix let vaccineData: VaccineDataState | undefined Object is possibly 'undefined', you could:

Add ! to tell the Typescript compiler that vaccineData will never be undefined (only if you are sure that it never actually is):

let vaccineData = vaccine.find(state => state.Location === 'WA')!;

or

Add some code to check for undefined:

    let vaccineData = vaccine.find(state => state.Location === 'WA');
    if(!vaccineData)
        return <p>Data not found!</p>;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery - how to find a specific JavaScript object inside an array within an object?

From Dev

Return an Array of Object and Function Typescript

From Dev

How to find an object with a specific property which is in the array of an array, by using NSPredicate?

From Dev

How to find an object with a specific property which is in the array of an array, by using NSPredicate?

From Java

How to use lodash to find and return an object from Array?

From Dev

How to set Object in array typescript

From Dev

How to return an object inside an array?

From Dev

find specific property inside array of object

From Dev

How to find the type of an object array

From Dev

json, how to return specific value and not the object

From Dev

How to find specific object in nested lists?

From Dev

how to find a specific item in the object is not a function in javascript

From Dev

How to merge two array object dynamically in typescript?

From Dev

How to convert JSON object to an Typescript array?

From Dev

How to return array of object to ajax call in php

From Java

how to return an object inside an array in json

From Dev

NodeRed how to return an array of msg object

From Dev

How to return with jquery an php json array object?

From Dev

How to return json object with json array inside of it?

From Dev

How to return array of object to ajax call in php

From Dev

How to return the main nodes of json as array of object?

From Dev

How to return object in an array with certain value with mongoose?

From Dev

how do I find out a JSON Object return JSON Array or string in android

From Dev

how do I find out a JSON Object return JSON Array or string in android

From Dev

How to check if array is unique on specific object property?

From Dev

How to sum values in array by specific object key?

From Dev

How to push a specific json object in an array?

From Dev

How to remove a specific object from an array

From Dev

Backbone how to get specific object in array

Related Related

  1. 1

    jQuery - how to find a specific JavaScript object inside an array within an object?

  2. 2

    Return an Array of Object and Function Typescript

  3. 3

    How to find an object with a specific property which is in the array of an array, by using NSPredicate?

  4. 4

    How to find an object with a specific property which is in the array of an array, by using NSPredicate?

  5. 5

    How to use lodash to find and return an object from Array?

  6. 6

    How to set Object in array typescript

  7. 7

    How to return an object inside an array?

  8. 8

    find specific property inside array of object

  9. 9

    How to find the type of an object array

  10. 10

    json, how to return specific value and not the object

  11. 11

    How to find specific object in nested lists?

  12. 12

    how to find a specific item in the object is not a function in javascript

  13. 13

    How to merge two array object dynamically in typescript?

  14. 14

    How to convert JSON object to an Typescript array?

  15. 15

    How to return array of object to ajax call in php

  16. 16

    how to return an object inside an array in json

  17. 17

    NodeRed how to return an array of msg object

  18. 18

    How to return with jquery an php json array object?

  19. 19

    How to return json object with json array inside of it?

  20. 20

    How to return array of object to ajax call in php

  21. 21

    How to return the main nodes of json as array of object?

  22. 22

    How to return object in an array with certain value with mongoose?

  23. 23

    how do I find out a JSON Object return JSON Array or string in android

  24. 24

    how do I find out a JSON Object return JSON Array or string in android

  25. 25

    How to check if array is unique on specific object property?

  26. 26

    How to sum values in array by specific object key?

  27. 27

    How to push a specific json object in an array?

  28. 28

    How to remove a specific object from an array

  29. 29

    Backbone how to get specific object in array

HotTag

Archive