2
export default {
  configuration:
  {
    site: {
      control:[
        api: {
          'list': '/api/v1/config/sites',
          'post': '/api/v1/config/sites/',
          'patch': '/api/v1/config/sites/',
          'delete': '/api/v1/config/sites/'
        }
      ],
    },
}

I am trying to access it by _.get(configuration, ['site','control','api','list'])

Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164
Abdulla Thanseeh
  • 9,438
  • 3
  • 11
  • 17

2 Answers2

2

control is an array, so you can't access control.api without first specifying which index in the array you want. Rather, you need to do:

_.get(configuration, ['site','control', '0', 'api','list'])
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
1

First thing is that your configuration structure is wrong, exactly in control:[api: {...}] the array doesn't support key/value structure like a literal object, so you should wrap that item by {} like control:[{api: {...}}] and access it like :

   _.get(configuration, ['site', 'control', '0', 'api', 'list'])

or transform your control value to an object like : control:{api: {...}}

the full example by wrapping api key by {}

let configuration = {
  site: {
    control: [{
      api: {
        'list': '/api/v1/config/sites',
        'post': '/api/v1/config/sites/',
        'patch': '/api/v1/config/sites/',
        'delete': '/api/v1/config/sites/'
      }
    }],
  }
}
var dumb = _.get(configuration, ['site','control','0','api','list'])

document.getElementById("key").innerHTML = dumb
console.log(_.get(configuration, ['site', 'control', '0', 'api', 'list']))
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
<p id="key"></p>
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164
  • still i am facing the issue to display it in p tag. Could you help me with the jsfiddle https://jsfiddle.net/thanseeh/2en7km3x/10/ – Abdulla Thanseeh Jan 17 '19 at 11:09
  • do like i did in my answer use `innerHTML` instead of value like `document.getElementById("key").innerHTML=dumb` and don't use export default check this https://jsfiddle.net/ynkja2Lw/ – Boussadjra Brahim Jan 17 '19 at 11:11