npm i object-end-keys -S
var options = {
separator: '.',
keyType: {
string: {
prefix: '',
suffix: ''
},
number: {
prefix: '[',
suffix: ']'
}
}
};
var keys = require('object-end-keys');
var data = {
"name": {
"first": "Joseph",
"last": "Raketov"
},
"age": 24,
"eyeColor": "red",
"friends": [
{
"id": 0,
"name": "Alex Shakirov"
}
]
};
keys(obj)
// ["name.first", "name.last", "age", "eyeColor", "friends[0].id", "friends[0].name"]
options = {
includeParent: true
}
keys(obj, options)
// ["name", "name.first", "name.last", "age", "eyeColor", "friends", "friends[0]", "friends[0].id", "friends[0].name"]
options = {
keyType: {
number: {
prefix: '.',
suffix: ''
}
}
};
keys(obj, options)
// ["name.first", "name.last", "age", "eyeColor", "friends.0.id", "friends.0.name"]
- support for browsers