dot-finder
dot-finder is using powerful dot notation to get values in JSON object
Table of Contents
Install
$ npm install dot-finder
Usage
const dot = require("dot-finder");
dot({ foo: { bar: "baz" } }, "foo.bar");
dot({ foo: { bar: "baz" } }, "foo['bar']");
dot({ "foo.bar": { baz: "hoge" } }, "foo\\.bar.baz");
dot({}, "notfound", "defaultValue");
dot([[{ fruits: "apple" }, { fruits: "pineapple" }]], "[0][1].fruits");
dot([[{ fruits: "apple" }, { fruits: "pineapple" }]], "0.1.fruits");
const data1 = {
authors: [
{ username: "tsuyoshiwada", profile: { age: 24 } },
{ username: "sampleuser", profile: { age: 30 } },
{ username: "foobarbaz", profile: { age: 33 } }
]
};
dot(data1, "authors.*.username");
dot(data1, "authors.*.profile.age");
const data2 = [
{
posts: {
tags: [
{ id: 1, slug: "news" },
{ id: 2, slug: "sports" },
{ id: 3, slug: "entertaiment" }
]
}
},
{
posts: {
tags: [
{ id: 4, slug: "music" },
{ id: 5, slug: "it" },
{ id: 6, slug: "programming" }
]
}
}
];
dot(data2, "*.posts.tags.*.slug");
Contribute
- Fork it!
- Create your feature branch: git checkout -b my-new-feature
- Commit your changes: git commit -am 'Add some feature'
- Push to the branch: git push origin my-new-feature
- Submit a pull request :D
License
MIT © tsuyoshiwada