MartinC
MartinC

Reputation: 55

How to access member value of multidimensional array if name is given as variable

Is it possible to access members of multidimensional array by having name of member as variable? So, one could change column by changing variable value and get the value of that column eventually.

Example Dataset

{
    "data": {
        "id": 2757, 
        "name": "Callisto Network", 
        "symbol": "CLO", 
        "website_slug": "callisto-network", 
        "rank": 472, 
        "circulating_supply": 431731687.0, 
        "total_supply": 479443453.0, 
        "max_supply": 6500000000.0, 
        "quotes": {
            "USD": {
                "price": 0.0183603, 
                "volume_24h": 37051.4, 
                "market_cap": 7926723.0, 
                "percent_change_1h": 0.15, 
                "percent_change_24h": -2.43, 
                "percent_change_7d": -9.42
            }, 
            "BTC": {
                "price": 2.7767e-06, 
                "volume_24h": 5.6034650942, 
                "market_cap": 1199.0, 
                "percent_change_1h": 0.01, 
                "percent_change_24h": -6.16, 
                "percent_change_7d": -7.0
            }
        }, 
        "last_updated": 1531748143
    }, 
    "metadata": {
        "timestamp": 1531747771, 
        "error": null
    }
}

Upvotes: 1

Views: 87

Answers (4)

Sasha K.
Sasha K.

Reputation: 287

Yes it is possible. On the object that you provided:

let myObject = {
    "data": {
        "id": 2757, 
        "name": "Callisto Network", 
        "symbol": "CLO", 
        "website_slug": "callisto-network", 
        "rank": 478, 
        "circulating_supply": 431633647.0, 
        "total_supply": 479321653.0, 
        "max_supply": 6500000000.0, 
        "quotes": {
            "USD": {
                "price": 0.0180211, 
                "volume_24h": 36733.5, 
                "market_cap": 7778513.0, 
                "percent_change_1h": 3.86, 
                "percent_change_24h": -5.71, 
                "percent_change_7d": -11.29
            }, 
            "BTC": {
                "price": 2.7279e-06, 
                "volume_24h": 5.5604246894, 
                "market_cap": 1177.0, 
                "percent_change_1h": 3.77, 
                "percent_change_24h": -9.24, 
                "percent_change_7d": -8.86
            }
        }, 
        "last_updated": 1531744784
    }, 
    "metadata": {
        "timestamp": 1531744413, 
        "error": null
    }
}

To access the name field from a variable on this object, you would do:

let x = 'name'

console.log(myObject.data[name]);

Notice instead of using a ., we are instead wrapping the variable name with an opening and closing bracket to access it.

"Callisto Network" would print out

You can learn more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

Upvotes: 0

MartinC
MartinC

Reputation: 55

thanks for help, it works :)

const variable = 'USD' // change it to BTC
alert(JSON.stringify(array.quotes[variable].price), true) 

Upvotes: 1

Adeel
Adeel

Reputation: 2959

You can set keys in an array and loop through it to get values dynamically, something like below

var array = {
  "data": {
        "id": 2757, 
        "name": "Callisto Network", 
        "symbol": "CLO", 
        "website_slug": "callisto-network", 
        "rank": 473, 
        "circulating_supply": 431584927.0, 
        "total_supply": 479260453.0, 
        "max_supply": 6500000000.0, 
        "quotes": {
            "USD": {
                "price": 0.0185867, 
                "volume_24h": 37855.1, 
                "market_cap": 8021740.0, 
                "percent_change_1h": 8.15, 
                "percent_change_24h": -2.81, 
                "percent_change_7d": -8.51
            }, 
            "BTC": {
                "price": 2.8148e-06, 
                "volume_24h": 5.7328503345, 
                "market_cap": 1215.0, 
                "percent_change_1h": 7.98, 
                "percent_change_24h": -6.41, 
                "percent_change_7d": -5.96
            }
        }, 
        "last_updated": 1531744663
    }, 
    "metadata": {
        "timestamp": 1531744326, 
        "error": null
    }
};

/* Specifying keys in array */
var key = ["USD", "BTC"];

for (key in array.data.quotes) {    
    /* Price of both keys */
    console.log(key + ' price is: ' + array.data.quotes[key].price);
    /*****************************************/
}

Or, you can simply pass the variable name like this

var key = "USD";
console.log(array.data.quotes[key].price);

Upvotes: 1

lomboboo
lomboboo

Reputation: 1233

Just use alternative object syntax like this:

data.quotes[$variable].price

Here is the example with your data jsfiddle

Upvotes: 0

Related Questions