0

I'm working with Javascript to build a mapping function, that, given a scheme should find the mapper's object's value within an application variable.

My attempt at doing this results in an undefined error and I'm trying to figure out what I need to change to make it work correctly, I've put together a JS fiddle that can be found here as well.

const application = {
  state: {
    outgoings: {
      credit: 0,
      food: 0
    }
  }
}

const mapper = {
  "AppFoodSpend": "outgoings.food",
  "AppCreditSpend": "outgoings.credit"
}

for (const field in mapper) {
    console.log(application.state[mapper[field]])
    // expected results should be credit 0 and food 0
}

For further context, I have an object called application which contains some fields, they may not always be in the same order. My mapper object contains some key/value pairs, and it's the value that I'd like to try and find in application. for for instance, my console log should be retrieving something like:

  • application.state.outgoings.food
  • application.state.outgoings.credit

They may not always be in this order, so I need to (on each iteration of the mapper) find the key in my application variable.

What do I need to change here?

Ryan H
  • 2,620
  • 4
  • 37
  • 109
  • It happen because JS _doesn't support_ property accessors separated by dot, so `application.state['outgoings.food']` _not_ equal to `application.state.outgoings.food`. How to tranform one type to another you can find here: https://stackoverflow.com/questions/6393943/convert-a-javascript-string-in-dot-notation-into-an-object-reference – Xeelley Nov 19 '21 at 10:59

1 Answers1

1

Strings like 'outgoings.food' are not valid to navigate a data structure. You could use some "lenses" lib or write something like this...

const mapper = {
  "AppFoodSpend": ["outgoings", "food"],
  "AppCreditSpend": ["outgoings", "credit"]
}

for (const field in mapper) {
    console.log(
        mapper[field].reduce(
            (ctx, path) => ctx[path],
            application.state
        )
    )
}
Fabiano Taioli
  • 5,270
  • 1
  • 35
  • 49
  • Ah great, this also works. This is great for getting a value, how could I possibly **set** a value this way? For instance, I'd like to try and set the value of `application.state.outgoings.food` for instance within my `for` loop there? I tried `mapper[field].split('.').reduce((o,i) => o[i], application.state)) = 'TEST'` which resulted in an error. – Ryan H Nov 19 '21 at 11:22