You can also use a handy lib called dot-object
.
Particularly dot.dot() method
var dot = require('dot-object');
var obj = {
id: 'my-id',
nes: { ted: { value: true } },
other: { nested: { stuff: 5 } },
some: { array: ['A', 'B'] }
};
var tgt = dot.dot(obj);
This will result in
{
"id": "my-id",
"nes.ted.value": true,
"other.nested.stuff": 5,
"some.array[0]": "A",
"some.array[1]": "B"
}
Link: https://github.com/rhalff/dot-object?tab=readme-ov-file#convert-object-to-dotted-keyvalue-pair