getting specific data from an object

SkyeBoniwell

I'm trying to figure out how to get the data I need out of an object I created.

I previously had my app data stored in 2 arrays like this:

const AVATARS = {
    avatar1: require('./images/avatar_1.jpg'),
    avatar2: require('./images/avatar_2.jpg')
}

export const STATS = {
    stats1: "\nHP: 124\n" +
            "INT: 23\n" +
            "CHR: 13\n",

    stats2: "\nHP: 423\n" +
            "INT: 24\n" +
            "CHR: 11\n"
}

To populate the app page, I would use these two functions to get the data:

import { AVATARS } from './arrays'
import { STATS } from './arrays'

//returns data based on array position(num)
export function getAvatar(num) {
    return AVATARS['avatar' + num];
}

export function getStats(num) {
    return STATS['stats' + num];
}

I recently decided to switch to an object where I store all the data, like this:

export var Characters = [
    {
        id: 1,
        Name: "Abe",
        HitPointValue: "124",
        StrengthValue: "12",
        IntelligenceValue: "14",
        WisdomValue: "16",
        DexterityValue: "12",
        ConstitutionValue: "10",
        CharismaValue: "17",
        Avatar: require('./images/avatar_7.jpg')
    },
    {
        id: 2,
        Name: "Jake",
        HitPointValue: "141",
        StrengthValue: "21",
        IntelligenceValue: "6",
        WisdomValue: "5",
        DexterityValue: "8",
        ConstitutionValue: "20",
        CharismaValue: "10",
        Avatar: require('./images/avatar_2.jpg')
    }
]

But now that I did that, I have no idea how to get the needed data. Obviously my old functions won't work. Is there a way to pick which data I need for a given page?

For example, if I only need the Name and Avatar for one page, but I might need the stats for another page.

Thanks!

Shubham Khatri

You can modify you functions to accept id like

import {characters} from './arrays';

//returns data based on array position(id)
export function getAvatar(id) {
    var idx = characters.findIndex((val) => val.id == id);

    return characters[idx].Avatar;
}

export function getName(id) {
     var idx = characters.findIndex((val) => val.id == id);
    return characters[idx].Name;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting specific data from an object C#

From Dev

Getting Specific Data from XML

From Dev

Getting specific data from html

From Dev

Getting Specific Data from XML

From Dev

getting the data from a specific ARRAY

From Dev

Getting specific data from array

From Dev

Getting data from JSON Object

From Dev

Getting data from specific datetime range

From Dev

Getting a specific category of data from firebase

From Dev

Getting Specific Data From the Following JSON?

From Dev

Getting data from specific datetime range

From Dev

Getting specific data from an excel sheet JAVA

From Dev

Getting only specific data from controller

From Dev

EJS getting specific data from MySQL

From Dev

Getting json from an URL and displaying specific data

From Dev

Getting specific cell data from SAP to excel

From Dev

Getting data from specific multiple columns

From Dev

Getting data from the function using an Object in CF

From Dev

Getting data from matplotlib axes object

From Dev

Getting data from an object type in c#

From Dev

Getting data from input to use as object in javascript

From Dev

KnockoutJS - Getting ViewModel data from a Nested Object

From Dev

Getting null data from a JSON object

From Dev

Getting data from the function using an Object in CF

From Dev

Getting data from an object in an ArrayList into GUI

From Dev

Getting the data from json object in javascript

From Dev

Error in getting data from JSON Object

From Dev

getting nested data from JSON array object

From Dev

Getting child data from a JSON object in angular