object-path icon indicating copy to clipboard operation
object-path copied to clipboard

Cannot set object with number in path e.g. 'a.2008'

Open aldo-sanchez opened this issue 6 years ago • 1 comments

I want to set an object as follows:

let emptyObj = {}
const path = 'a.2008'
const value = [1,2,3,4]
objectPath.set(emptyObj, path, value)

I expect the object to look like:

emptyObject = {
  a: {
    2008: [1, 2, 3, 4],
  }
}

Instead the object looks like this:

{
    "a": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        ...(2008x),
       [1, 2, 3, 4]
    ],
}

aldo-sanchez avatar Mar 20 '18 15:03 aldo-sanchez

use an array path when you expect no conversions. by convention, when there's a number in a string path, it always mean an array index. so in your example:

let emptyObj = {}
const path = ['a', '2008'] // string '2008' really means string, thus an object property
const value = [1,2,3,4]
objectPath.set(emptyObj, path, value)

pocesar avatar Mar 20 '18 16:03 pocesar