js-select
js-select uses js-traverse to traverse and modify JavaScript object nodes that match JSONSelect selectors:
var select = require("js-select");
var people = {
george: {
age : 35,
movie: "Repo Man"
},
mary: {
age: 15,
movie: "Twilight"
}
}
select(people, ".age").forEach(function(age) {
this.update(age - 5);
})
select(people, ".age").nodes();
See js-traverse for all the things you can do to modify the node. The forEach()
callback will get the same this
context as the forEach()
callback from js-traverse, plus a this.matches()
which will test if the node matches a selector:
select(obj).forEach(function(node) {
if (this.matches(".mary > .movie")) {
this.update(node.toLowerCase());
}
}
selectors
js-select supports the following JSONSelect selectors so far:
"*"
".key"
".ancestor .key"
".parent > .key"
":root"
":nth-child(n)"
":nth-child(even)"
":nth-child(odd)"
":first-child"
install
Download the code, then with npm:
npm install js-select
Get a browser file after npm-installing using browserify