Utility to manage an object property using simple 'a.b.c' paths (with dot–notation).
npm install easy-object-selector
Path | Description |
---|---|
a.b.c | The value of 'c' |
["a", "b", "c"] | The value of 'c' |
a.d.1.e | The second value of 'e' |
a.d.*.e | All values of 'e' |
a.d.first().e | The first value of 'e' |
a.d.last().e | The last value of 'e' |
const selector = require("easy-object-selector");
const obj = {
a: {
b: {
c: "val1"
},
d: [
{
e: "val2"
},
{
e: "val3"
}
]
}
};
const select = selector.select;
select(obj, "a.b.c"); // => "val1"
select(obj, ["a", "b", "c"]); // => "val1"
select(obj, "a.b.x", "defValue"); // => "defValue"
select(obj, "a.d.0.e"); // => "val2"
select(obj, "a.d.*.e"); // => ["val2", "val3"]
select(obj, "a.d.first().e"); // => "val2"
select(obj, "a.d.last().e"); // => "val3"
const has = selector.has;
has(obj, "a.b.c"); // => true
const keys = selector.keys;
keys(obj, "a"); // => ["b", "d"]
const put = selector.put;
const o = {};
put(o, "a.b.c", "val1") // => {a:{b:{c:"val1"}}}
const wrapper = selector.wrap(obj);
wrapper.get("a.b.c"); // => "val1"
wrapper.has("a.b.c"); // => true
wrapper.keys("a"); // => ["b", "d"]
const o2 = {};
const wrapper2 = selector.wrap(o2);
wrapper2.put("a.b.c", "val1"); // => {a:{b:{c:"val1"}}}
Get the value of the selected property if it exists.
obj
Object source objectselector
(String | Array) selectordefValue
(Object | Array | String | Number | Boolean) default value
const selector = require("easy-object-selector");
const select = selector.select;
const obj = {
a: {
b: {
c: "val1"
},
d: [
{
e: "val2"
},
{
e: "val3"
}
]
}
};
select(obj, "a.b.c"); // => "val1"
select(obj, ["a", "b", "c"]); // => "val1"
select(obj, "a.b.x"); // => undefined
select(obj, "a.b.x", "defValue"); // => "defValue"
select(obj, "a.d.0.e"); // => "val2"
select(obj, "a.d.1.e"); // => "val3"
select(obj, "a.d.*.e"); // => ["val2", "val3"]
select(obj, "a.d.first().e"); // => "val2"
select(obj, "a.d.last().e"); // => "val3"
Returns (Object | Array | String | Number | Boolean | undefined)
Check if the selected property exists.
const selector = require("easy-object-selector");
const has = selector.has;
const obj = {
a : {
b : {
c: "val1"
}
}
};
has(obj, "a.b.c"); // => true
has(obj, ["a", "b", "c"]); // => true
has(obj, "a.b.x"); // => false
Returns Boolean
Returns an array of keys.
const selector = require("easy-object-selector");
const keys = selector.keys;
const obj = {
a : {
b : {
c: "val1",
d: "val2",
e: "val3",
}
}
};
keys(obj, "a.b"); // => ["c", "d", "e"]
keys(obj, ["a", "b"]); // => ["c", "d", "e"]
keys(obj, "a.b.x"); // => []
keys(obj, "a.b.x", ["a", "b"]); // => ["a", "b"]
Returns Array
Put the value in the object property.
obj
Object source objectselector
(String | Array) selectorvalue
(Object | Array | String | Number | Boolean) value
const selector = require("easy-object-selector");
const put = selector.put;
const o = {};
put(o, "a.b.c", "val1") // => {a:{b:{c:"val1"}}}
const o2 = {};
put(o2, ["a", "b", "c"], "val1") // => {a:{b:{c:"val1"}}}
Returns Object
Creates a new ObjectWrapper
obj
Object object to wrap
const selector = require("easy-object-selector");
const obj = {
a: {
b: {
c: "val1"
}
}
};
const wrapper = selector.wrap(obj);
wrapper.get("a.b.c"); // => "val1"
wrapper.get(["a", "b", "c"]); // => "val1"
Returns ObjectWrapper wrapper
Object wrapper
obj
Object object to wrap
Get the value of the selected property if it exists.
selector
(String | Array) selectordefValue
(Object | Array | String | Number | Boolean) default value
const selector = require("easy-object-selector");
const obj = {
a: {
b: {
c: "val1"
},
d: [
{
e: "val2"
},
{
e: "val3"
}
]
}
};
const wrapper = selector.wrap(obj);
wrapper.get("a.b.c"); // => "val1"
wrapper.get(["a", "b", "c"]); // => "val1"
wrapper.get("a.b.x"); // => undefined
wrapper.get("a.b.x", "defValue"); // => "defValue"
wrapper.get("a.d.0.e"); // => "val2"
wrapper.get("a.d.1.e"); // => "val3"
wrapper.get("a.d.*.e"); // => ["val2", "val3"]
wrapper.get("a.d.first().e"); // => "val2"
wrapper.get("a.d.last().e"); // => "val3"
Returns (Object | Array | String | Number | Boolean | undefined)
Check if the selected property exists.
const selector = require("easy-object-selector");
const obj = {
a : {
b : {
c: "val1"
}
}
};
const wrapper = selector.wrap(obj);
wrapper.has("a.b.c"); // => true
wrapper.has(["a", "b", "c"]); // => true
wrapper.has("a.b.x"); // => false
Returns Boolean
Returns an array of keys.
const selector = require("easy-object-selector");
const obj = {
a : {
b : {
c: "val1",
d: "val2",
e: "val3",
}
}
};
const wrapper = selector.wrap(obj);
wrapper.keys("a.b"); // => ["c", "d", "e"]
wrapper.keys(["a", "b"]); // => ["c", "d", "e"]
wrapper.keys("a.b.x"); // => []
wrapper.keys("a.b.x", ["a", "b"]); // => ["a", "b"]
Returns Array
Put the value in the object property.
const selector = require("easy-object-selector");
const o = {};
const wrapper = selector.wrap(o);
wrapper.put("a.b.c", "val1") // => {a:{b:{c:"val1"}}}
Returns Object