0

I have the following object:

const updatedFields = {
    "user.name": "potato",
    "plan": "diamond"
}

I expect it to be:

const obj = {
    user: {
        name: "potato"
    },
    plan: "diamond"
}

The code I am currently running:

const updatedFields = {
    "user.name": "potato",
    "plan": "diamond"
}

const obj = {}

setpath = ([p,...ps], v, o) => ps.length ? setpath(ps, v, o[p]) : o[p] = v
Object.entries(updatedFields).forEach(([p,v]) => setpath(p.split("."), v, obj))

console.log(obj)

The error I'm getting:

setpath = ([p,...ps], v, o) => ps.length ? setpath(ps, v, o[p]) : o[p] = v
                                                                       ^

TypeError: Cannot set property 'name' of undefined
    at setpath (/tmp/8LYm4RhWvR.js:8:72)
    at setpath (/tmp/8LYm4RhWvR.js:8:44)
    at Object.entries.forEach (/tmp/8LYm4RhWvR.js:9:50)
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/tmp/8LYm4RhWvR.js:9:31)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

How can I solve it?

jhnxx
  • 93
  • 1
  • 6
  • 1
    Please refer to: [Convert javascript dot notation object to nested object](https://stackoverflow.com/questions/7793811) and [Fastest way to flatten / un-flatten nested JavaScript objects](https://stackoverflow.com/questions/19098797). If your want to resolve your issue with the code you've posted, we can keep this question. If you are okay with any solution for creating the nested object, we can close it as a duplicate of this question. – adiga Sep 29 '22 at 06:38

1 Answers1

1

o[p] or the user is undefined initially. So, when you try to set name property of the user object, it will throw an error.

You can use the Logical nullish assignment to set a value, if it doesn't exist. So, set o[p] ??= {}

const updatedFields = {
    "user.name": "potato",
    "plan": "diamond"
}

const obj = {}

const setpath = ([p,...ps], v, o) => ps.length ? setpath(ps, v, o[p] ??= {}) : o[p] = v
Object.entries(updatedFields).forEach(([p,v]) => setpath(p.split("."), v, obj))

console.log(obj)

Also, there are alternatives this here:

adiga
  • 34,372
  • 9
  • 61
  • 83
  • It works here on the "Run code snippet" button, but when compiling it, it is giving me the following error: – jhnxx Sep 29 '22 at 06:49
  • const setpath = ([p,...ps], v, o) => ps.length ? setpath(ps, v, o[p] ??= {}) : o[p] = v ^ SyntaxError: Unexpected token ? – jhnxx Sep 29 '22 at 06:49
  • The error is pointing to the " ??= " – jhnxx Sep 29 '22 at 06:50
  • @jhnxx The syntax is not yet supported in your environment. You could replace it with `setpath(ps, v, o[p] = o[p] || {})` – adiga Sep 29 '22 at 06:50
  • Well, I think its the javascript version to be honest. Just tried another compiler and worked fine as well. So thank you! – jhnxx Sep 29 '22 at 06:52