-3

I have some data that looks like this:

obj = {
    pref: {
      language: 'English',
    }
};

I want to change the value of language to 'Spanish'

Rumbo2019
  • 13
  • 1
  • 2
    `obj.pref.language = ???` What exactly is the issue? Basic syntax? – Shilly Feb 26 '19 at 15:07
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383) – adiga Feb 26 '19 at 15:15

2 Answers2

3

You can simply use obj.pref.language = 'Spanish' or obj['pref']['language'] = 'Spanish'.

NilsH
  • 149
  • 2
  • 5
1

Use dot notation to access the key and change the value

let obj = {
  pref: {
    language: 'English',
  }
};

obj.pref.language = 'Spanish';

console.log(obj)
brk
  • 48,835
  • 10
  • 56
  • 78