0

I am trying to build an object path based upon a list that I am being passed. I have set up an example of what I mean here :

https://jsfiddle.net/alexjm/kypwht31/6/

var state = { irc : { main : "test"}};

var key = ["irc", "main"];


createStateFromKey = function (key) {
    var stateForBuild = "state";
    _.each(key, function(n, key) {
    stateForBuild += "." + n;
  });

  return stateForBuild;
};


console.log(createStateFromKey(key));

this sends me back the string of 'state.irc.main' which is what I want, but I want it to be an evaluation of the actual object - "test". I am being passed this list that can have any number of items, and need to search inside that state using the key list i am being provided with. I am using it in a function like so :

function mapStateToProps(key, state) {

return {
    test: state.???.loadingJSON
 };
}

so it needs to evaluate on that state, something like this:

return {
    test: state[key[0]].[key[1]].loadingJSON
 };

I am trying to run it into a for each because the length of that list is dynamic. I am not entirely sure I am approaching this correctly because that function is returning a string that i am not sure is working. Any input would be appreciated. Thanks!

ajmajmajma
  • 13,712
  • 24
  • 79
  • 133

0 Answers0