0

I apologize in advance, since I realize that this should be fairly easy to solve, but for the love of me I can't make this work.

I have a json object structured like so:

settings:{
  prop_one:{
    desktop:{
      aaa:'',
      bbb:'',
      ccc:''
    },
    mobile:{
      aaa:'',
      bbb:'',
      ccc:''
    }
  }
}

next I have a function that receives the key I want to change and the parent keys in dot notation, ie.

myFunc('aaa', 'prop_one.desktop')

inside of that function what I want to do is to change the property of the given value. since I can access json object using simple array notation in theory this would look like so:

myJson[prop_one][desktop][aaa] = 'some new value'

but I struggle here - I simply cannot figure this out, I think my brain went on holidays. Any help how to deal with this will be hugely appreciated

myJson = {/* structure as bove*/}
function myFunc(parent, key)
{
//what do I do here
}

EDIT: as pointed out to me, the object in question is JS object not JSON - obviously I took an (incorrect) shortcut here - my bad

Peter
  • 181
  • 2
  • 12

1 Answers1

2
myJson = {/* structure as above*/}
function myFunc(parent, key)
{
  const value = key.split('.').reduce((o,i)=>o[i], myJson['settings']);
  value[parent] = 'some new value';
  return myJson; 
}
kayuapi_my
  • 498
  • 1
  • 6
  • 9