object-path
object-path copied to clipboard
Cannot set object with number in path e.g. 'a.2008'
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]
],
}
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)