Search code examples
javascriptnested-object

Get value of object inside of array which is inside an object itself


I have a quick question which I seem to be tripped up on.

I have a data structure like so:

data: {
   accounts: [{
      info: false
   }]
}

Is there a "fancy" React way for me to get the value of the boolean associated with the info key?

I can of course retrieve the value with using map or find, but it always ends up being somewhat convoluted or involved code.

If I do something like

const { accounts } = data;
const customer = accounts.map(a => a.info);

The value for customer always ends up coming back as [false] instead of just false, which is really confusing me, because I am not sure why it would come back inside of an array, where it is not an array to begin with, and it is being mapped out of an array (accounts).

I had the same result when using forEach and find.

Is there something I'm missing? There has to be a quick Reactive one-liner to get the boolean value I'm looking for, and set it to a variable.

Anyone...?


Solution

  • data.accounts[0].info
    

    or

    Using underscore.js

    _.first(data.accounts).info